11

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

  1. What is the replacement of QInputContext in Qt5? (Because I read somewhere about onscreen keybord by implementing QInputContext but this is not supported by Qt 5.)

  2. Where can I find QPlatformInputContext and QInputPanel (on an internet search I found these two as alternatives of QInputContext but not sure about that and also I was unable to find them)?

My requirements:

  1. Keyboard will not use QML or any external library (already build other keyboards).

  2. Keyboard will use Qt Gui (traditional).

tanius
  • 14,003
  • 3
  • 51
  • 63
Jai
  • 1,292
  • 4
  • 21
  • 41

5 Answers5

5

I understand there are two challenges you would have:

  1. Getting notified as to when to show/hide the on-screen keyboard, based on the focus being on text widgets
  2. How to post key-press event to the text widgets

ANSWER

  1. As for the former, you could use QObject::InstallEventFilter() on widgets that you want to provide the keyboard service to. You can then look for the mouseReleaseEvent along the lines of the Qt code in the link.
  2. This can be achieved by using QCoreApplication::postEvent()

As for QPlatformInputContext, get the example of a Qt Virtual Keyboard here.

user1055604
  • 1,624
  • 11
  • 28
  • 1
    For auto hide i used focusChanged(QWidget* old,QWidget* new) signal of QApplication.for puting text in QLineEdit/QTextEdit, I have posted key event in slot which is connected to button click signal of keyboard button. So no need to write extra for input text.It is working fine and it is generic too. – Jai Apr 09 '14 at 09:29
4

I took me quite a while to find out how to do this in QT5 without qml and too much work. So thought I'd share:

#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 clue here is that by clicking buttons (if you would manually make your keyboard), launches a sendevent to the current object thas has focus (for example a textbox). You could of course hardcode a textbox, but that only works if you have only a single input to use your keyboard for.

The last thing you have to make sure, is to set the focusPolicy of your keyboard buttons to NoFocus, to prevent focus from shifting when the keyboard is pressed.

Credits go to https://www.wisol.ch/w/articles/2015-07-26-virtual-keyboard-qt/

Hope this helps someone.

Arnout
  • 341
  • 2
  • 8
  • It seems that using "QKeySequence(key).tostring()" doesn't always produce the desired results, in particular using it on letters always seems to produce capitals. – plugwash Nov 04 '19 at 21:08
2

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

  • Good find! It comes with [full source code on Github](https://github.com/tolszak/MockupVirtualKeyboard). – tanius Jul 19 '20 at 18:17
1

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, the QInputMethod docs don't say much about how to access it from c++. You have to get an instance from QGuiApplication, like I did above.

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
1

Qt now provides a virtual keyboard framework in Qt 5.5.

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

I have not tried it, so I can't say how easy it is to use. It looks like it's QML-based.

(It says it's for Linux and boot2qt, but it can also be built for Windows according to the building page (http://doc.qt.io/QtVirtualKeyboard/build.html))

michaelmoo
  • 365
  • 4
  • 11
  • 6
    One should note that QtVirtualKeyboard is licensed either commercially or via GPL, different to the rest of Qt, which might keep people from using it. – FourtyTwo Nov 14 '17 at 09:15