Qt5 Virtual Keyboard C++ Integration and Implementation
reference:
I. Download Source Code
Go to the official github to download the required version of the source code:
https://github.com/qt/qtvirtualkeyboard/releases
The version I downloaded is:
https://github.com/qt/qtvirtualkeyboard/archive/v5.10.0.tar.gz
II. Settings
-
Configure the required language
1). Configured by Qt Creator
Open the Qt project file and open the left side of the projectProjects->Build->Build Steps->qmake->Additional arguments
Add configuration parameters inAdditional arguments
:CONFIG+="lang-ar_AR lang-da_DK lang-de_DE lang-en_GB lang-es_ES lang-fa_FA lang-fi_FI lang-fr_FR lang-hi_IN lang-it_IT lang-ja_JP lang-ko_KR lang-nb_NO lang-pl_PL lang-pt_PT lang-ru_RU lang-sv_SE lang-zh_CN lang-zh_TW"
Select the desired language as needed, of course, if you simply configure it for all languages, then as the following:
CONFIG+=lang-all
-
Configure the required language
2). Configure and generate the makefile directly from the command line./opt/Qt5.10.1/5.10.1/gcc_64/bin/qmake qtvirtualkeyboard.pro -spec linux-g++ 'CONFIG+=lang-all'
Specify the linux platform with the
-spec
parameter. -
The virtualkeyboard.pro configuration file add the following content:
LIBS+=-L../../lib
The reason for this is because when integrating Chinese language, Japanese, etc. with the language of the three-party library, the generated three-party library will not be found when the qtvirtualkeyboard is finally generated. We only need to add a path to solve this problem.
III. C++ integration
In order to use Qt Virtual Keyboard in the traditional QWidget program (corresponding to QML), we only need to add the following code at the entry of the program
:
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
Finally, the program looks like this
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
IV. Customize keyboard size and location
We need to change the InputPanel.qml file
Item {
id: inputPanel
property int screenHeight: Screen.desktopAvailableHeight;
anchors.fill: parent;
Keyboard {
id: keyboard
anchors.horizontalCenter: parent.horizontalCenter
width: Screen.desktopAvailableWidth * 2 / 3
y: getInputY()
function getInputY(){
return 0
}
}
}
The sample code is shown above, defining the horizontal center position
and width
of the keyboard. The vertical position of the keyboard is set by y
and returned by the getInputY()
function. I does not give a concrete implementation. A more general idea can be according to the position of the input box to adapt the position of the keyboard
, of course, this also needs to change the c++ code of the qtvirtualkeyboard related files, I will not be described here.
V. More Qt5 Virtual Keyboard Settings
- Qt5 Virtual Keyboard C ++ Integration and Implementation II (Adaptive Position)
- Qt5 Virtual Keyboard C ++ Integration and Implementation III (Solving the Problem of Modal Dialog Keyboard Invalidation)