On-screen keyboard in Qt 5

I want to create an on-screen keyboard for a desktop application. The application will be built in Qt 5. I have a couple of questions, please clarify them.

  • What is a QInputContext replacement in Qt5? (Because I read somewhere about the screen key block implementing QInputContext , but this is not supported by Qt 5.)

  • Where can I find the QPlateformInputContext and QInputPanel (in an Internet search, I found these two as an alternative to QInputContext , but I’m not sure about it, and also could not find them)?

My requirements:

  • The keyboard will not use QML or any external library (already create other keyboards)
  • The keyboard will use Qt Gui (traditional)
+6
source share
5 answers

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.

0
source

I understand that you have two problems:

  • Notification of when to show / hide the on-screen keyboard based on focus on text widgets
  • How to send a keystroke event to text widgets

ANSWER


As for the QPlatformInputContext , take the Qt Virtual Keyboard here .

+2
source

A good example is given here http://tolszak-dev.blogspot.com.tr/2013/04/qplatforminputcontext-and-virtual.html uses Qt Quick for the on-screen keyboard. You can check it out.

+1
source

Qt now provides virtual keyboard infrastructure in Qt 5.5.

http://doc.qt.io/QtVirtualKeyboard/

I have not tried, so I can’t say how easy it is to use. This is similar to QML-based.

(He talks about it for Linux and boot2qt, but can also be created for Windows according to the building page ( http://doc.qt.io/QtVirtualKeyboard/build.html ))

+1
source

I just got this working in my awesome Qt app. Here is how I did it.

For Android and iOS:

 QObject::connect(lineEdit, SIGNAL(returnPressed()), qApp->inputMethod(), SLOT(hide())); 

For iOS:

Subclass QLineEdit and add the following:

 void focusOutEvent(QFocusEvent * fe) { QLineEdit::focusOutEvent(fe); #ifdef Q_OS_IOS if(fe->reason() == Qt::OtherFocusReason) { // Done was pressed! emit returnPressed(); } #endif } 

Btw, QInputMethod docs don't talk about how to access it from C ++. You should get an instance from QGuiApplication, as I did above.

Hope this helps.

0
source

Source: https://habr.com/ru/post/954492/


All Articles