I took some time to learn how to do this in QT5 without qml and too much work. So I thought:
#include <QCoreApplication> #include <QGuiApplication> #include <QKeyEvent> void MainWindow::on_pushButton_clicked() { Qt::Key key = Qt::Key_1;; QKeyEvent pressEvent = QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, QKeySequence(key).toString()); QKeyEvent releaseEvent = QKeyEvent(QEvent::KeyRelease, key, Qt::NoModifier); QCoreApplication::sendEvent(QGuiApplication::focusObject(), &pressEvent); QCoreApplication::sendEvent(QGuiApplication::focusObject(), &releaseEvent); }
The key here is that by pressing the buttons (if you manually make your keyboard), it starts sendevent to the current object that has focus (for example, a text field). Of course, you could hardcode the text box, but this only works if you have only one input to use your keyboard.
The last thing you need to do is set the focusPolicy of your keyboard buttons to NoFocus to prevent the focus from shifting when you press the keyboard.
Credits go to https://www.wisol.ch/w/articles/2015-07-26-virtual-keyboard-qt/
Hope this helps someone.
source share