Solved Using event filters as a class
-
On my mainwindow I have 6 QLineEdits, 2 QTextEdits, and a virtual keyboard I created. Currently I installed an even filter on each edit field so that when it has focus, the keyboard will recognize which edit field to how the characters being inputted by the keyboard. The following code is currently on my mainwindow and it does work.
void MainWindow::obtainEditFields() { _lineedits = ui->_stackedWidget->currentWidget()->findChildren<QLineEdit *>(); _textedits = ui->_stackedWidget->currentWidget()->findChildren<QTextEdit *>(); for (int i=0; i<_lineedits.count(); i++) _lineedits.at(i)->installEventFilter(this); for (int i=0; i<_textedits.count(); i++) _textedits.at(i)->installEventFilter(this); } bool MainWindow::eventFilter(QObject *target, QEvent *) { if(target->objectName().contains("TextEdit")) { for (int i=0; i<_textedits.count(); i++) { if(_textedits.at(i)->hasFocus()) ui->_keyboard->m_focusItem = _textedits.at(i); } } else { for (int i=0; i<_lineedits.count(); i++) { if(_lineedits.at(i)->hasFocus()) ui->_keyboard->m_focusItem = _lineedits.at(i); } } return false; }
However, I would like to create a separate class for this specific code. I created a class called EditFields.cpp which is now being called by mainwindow. Obviously right now is not working since the variables lineEdits and textEdits are not being recognized on the eventFilter. Is there a way to make them recognized?
#include "editfields.h" #include <QEvent> #include <QFocusEvent> EditFields::EditFields() : QObject() { keyboard = new Keyboard; } void EditFields::getEditFields(QList<QLineEdit *> lineEdits, QList<QTextEdit *> textEdits) { _lineEdit_count = lineEdits.count(); _textEdit_count = textEdits.count(); for (int i=0; i<_lineEdit_count; i++) { lineEdits.at(i)->installEventFilter(this); } for (int i=0; i<_textEdit_count; i++) { textEdits.at(i)->installEventFilter(this); } } bool EditFields::eventFilter(QObject *target, QEvent *event) { if(target->objectName().contains("TextEdit")) { for (int i=0; i<_textEdit_count; i++) { if(textEdits.at(i)->hasFocus()) keyboard->m_focusItem = textEdits.at(i); } } else { for (int i=0; i<_lineEdit_count; i++) { if(lineEdits.at(i)->hasFocus()) keyboard->m_focusItem = lineEdits.at(i); } } return false; }
-
Hi,
You would need to keep a copy of these list in your class. However, why not create a QPlatformInputContext ? See this article. That will avoid the custom filtering.
-
@SGaist I am using the keyboard as a widget which is currently in a stackedwidget. The reasoning is because the keyboard has to be included with the application I am creating.
-
If you take a look at the article you'll see that it proposes to use a custom keyboard widget (a bit like yours) which will be included with the application.
-
@SGaist I was reading it and I can't quite understand what is the purpose of QPlatformInputContext?
-
That's the piece that makes your keyboard appear when you click on an input widget and ensure the event is sent to the appropriate widget
-
@SGaist Ohhh my keyboard is always there regardless whether the QLineEdit is clicked or not
-
You can keep it visible, the thing is that with it you won't need to handle yourself the active input widget.