0

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");}
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • You can find out which widget has focus with [`QApplication::focusWidget`](http://qt-project.org/doc/qt-5/qapplication.html#focusWidget). – thuga Oct 28 '14 at 09:51
  • 1
    *"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](http://qt-project.org/doc/qt-5/qmessagebox.html) to find out? – HostileFork says dont trust SE Oct 28 '14 at 10:15
  • Note also that clicking a button may change the focus, [see this question](http://stackoverflow.com/questions/2011423/) for issues about focus policy. You might check to see if that happened in your click. – HostileFork says dont trust SE Oct 28 '14 at 10:21
  • Yes it is getting called, I tested that with a TextEdit.setText(), I will check this post – Abdou Abderrahmane Oct 28 '14 at 12:26
  • Also see [Onscreen Keyboard in Qt 5](https://stackoverflow.com/q/18979015/608639) and [What is an Input Method and what do we need it for?](https://www.kdab.com/qt-input-method-depth/) I'm still trying to figure out how to use a custom virtual keyboard to input text to the `QTextEdit`, though. – jww Dec 16 '19 at 23:36

2 Answers2

0

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
    ...
}
...
0

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.

Nithish
  • 1,580
  • 12
  • 21
  • Or just set the [focus policy](http://qt-project.org/doc/qt-5/qwidget.html#focusPolicy-prop) of the buttons to [`Qt::NoFocus`.](http://qt-project.org/doc/qt-5/qt.html#FocusPolicy-enum). – thuga Oct 28 '14 at 12:43
  • @thuga, Yes. Even that if he is ok with it. :) If for whatever reason he needs to do anything on the QPushButton::focusInEvent, then he will have to maintain a pointer. – Nithish Oct 28 '14 at 12:48
  • Hum, I'm really new to Qt, can you describe the process or link me some similar work ? – Abdou Abderrahmane Oct 28 '14 at 13:39
  • I hope you are familiar with C++. You need to write a class inheriting `QTextEdit` and implement the protected virtual method `focusInEvent`. Next you will have to replace your normal `QTextEdit`s with objects of the inherited class you created. Now whenever you click on a textedit, the function `focusInEvent` will be called. In this function you can point a public or static pointer variable of MainWindow class to the textedit instance. In your MainWindow use this pointer to access the `QTextEdit` methods. You can also try thuga's suggestion which is easier. – Nithish Oct 28 '14 at 17:07
  • Thank you, this helps a lot, I will try it, one more thing, If I'm looking to connect my GUI with a python back-end software, should I be doing this part in C++ or do the conversion to PyQT first ? – Abdou Abderrahmane Oct 28 '14 at 23:21
  • If your backend is in python, you can use PyQt. You can find something similar to what I explained in this [link]( http://qt-project.org/forums/viewthread/21474). Also you need to accept an answer if you think that answers your question sufficiently. – Nithish Oct 29 '14 at 01:35
  • @thuga I suggested this because, I had to go with this method in one of my old projects as there were other widgets which were brought to focus on certain events to notify the user. Thus even though i would have opted for `Qt::NoFocus` for the push buttons, `QApplication::focusWidget` would give me the other widget instead of the line edit/text edit. – Nithish Oct 29 '14 at 06:16
  • I'm not saying your answer is wrong. I was just giving another option in case buttons taking focus was a problem. – thuga Oct 29 '14 at 07:55