Search code examples
qton-screen-keyboard

How to get selected QTextEdit using on-screen keyboard?


I have to make a GUI for a touch screen software. It's on the same window as the QTextEdit. I was thinking of something simple with a limited set of characters (I also have to make PIN Pads for other windows later).

The approach I'm thinking of is hard-coding the text modifications done by each button. The problem I'm facing getting the QTextEdit that actually has the focus (is selected by the user's cursor).

So I would like to know how I could find out if a certain QTextEdit currently has focus or not ?

Also if there are better ways to do this whole thing ?


Here is my new code, what's wrong with it ?

#include "settings2.h"
#include "ui_settings2.h"

Settings2::Settings2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Settings2)
{
    ui->setupUi(this);
}

Settings2::~Settings2()
{
    delete ui;
}

void Settings2::on_q_btn_clicked()
{
    QTextEdit *textedit = qobject_cast<QTextEdit*>(QApplication::focusWidget());
    if(textedit){
    textedit->setText("aze");}
}

Solution

  • The way you are trying to get the QTextEdit in focus is wrong. Moreover as soon as you click on a button on your on-screen keyboard, the focus will move to the key and will not stay on the QTextEdit.

    I would suggest using a pointer to hold address of modified QTextEdit as soon as one comes to focus. Thus you will always know which was the last text edit in focus and keep appending the new text to that.

    You will have to write your own class inheriting QTextEdit and implement the QTextEdit::focusInEvent where you will be pointing the above mentioned pointer to the this pointer.