0
votes

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");}
}
2
You can find out which widget has focus with QApplication::focusWidget. - thuga
"What's wrong with it" Not enough information to know. Is on_q_btn_clicked() getting called? Can you set a breakpoint or pop up a QMessageBox to find out? - HostileFork says dont trust SE
Note also that clicking a button may change the focus, see this question for issues about focus policy. You might check to see if that happened in your click. - HostileFork says dont trust SE
Yes it is getting called, I tested that with a TextEdit.setText(), I will check this post - Abdou Abderrahmane
Also see Onscreen Keyboard in Qt 5 and What is an Input Method and what do we need it for? I'm still trying to figure out how to use a custom virtual keyboard to input text to the QTextEdit, though. - jww

2 Answers

0
votes

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.

0
votes

Per @thuga's comment QApplication::focusWidget.

If you want to be sure the focused widget is a certain category of widget you can use qobject_cast, which will only return a non-null pointer if that cast is valid:

QLineEdit *lineedit = qobject_cast<QLineEdit*>(widget);
QTextEdit *textedit = qobject_cast<QTextEdit*>(widget);
...
if (lineedit) {
    // do QLineEdit stuff with lineedit
    ...
}
if (textedit) {
    // do QTextEdit stuff with textedit
    ...
}
...