在过去的几天里,我一直在努力将QtVirtualKeyboard纳入我基于QWidget的应用程序,该应用程序运行在带有7“触摸屏显示的Raspberry上。
以下是我迄今所做的工作:
安装了插件:
sudo apt-get install -y qtvirtualkeyboard-plugin
sudo apt-get install -y qml-module-qtquick-controls2
sudo apt-get install -y qtdeclarative5-dev
sudo apt-get install qml-module-qt-labs-folderlistmodel添加了QT_IM_MODULE环境变量并将其设置为qtvirtualkeyboard
将QT += quickwidgets添加到我的.pro
创建了一个QQuickWidget来放置虚拟键盘。
.h
private:
QQuickWidget *m_quickWidget;.cpp
// In constructor
QUrl source(QML_FILE_PATH + "virtualkeyboard.qml");
m_quickWidget->setSource(source);
m_quickWidget->setAttribute(Qt::WA_AcceptTouchEvents);
ui->verticalLayout->addWidget(m_quickWidget);最后我的virtualkeyboard.qml文件
import QtQuick 2.7
import QtQuick.VirtualKeyboard 2.1
Rectangle {
id: window
width: 600
height: 0
InputPanel {
id: inputPanel
width: window.width
states: State {
name: "visible"
when: inputPanel.active
PropertyChanges {
target: window
height: inputPanel.height
}
}
transitions: Transition {
from: ""
to: "visible"
reversible: true
ParallelAnimation {
NumberAnimation {
properties: "y"
duration: 250
easing.type: Easing.InOutQuad
}
}
}
}
}所以到目前为止,视觉上一切看起来都很好。当我打开我的应用程序时,键盘小部件是不可见的(qml中的窗口高度:0),当我双击QTableView中的一个单元格(它的标志是Qt::ItemIsEnabled::ItemIsEnabled)时,键盘小部件显示在我垂直布局的底部,位置和大小都是正确的。
现在我的问题是:
InputContext::sendKeyClick(): no focus to send key click - QGuiApplication::focusWindow() is: QWidgetWindow(0x1e68250, name="ConfigWindow"),其中ConfigWindow是设计器表单中基本小部件的名称。editTriggers设置为CurrentChanged。我知道这是因为如果我单击我的单元格,光标开始闪烁,如果我使用物理键盘连接到我的覆盆子,我可以编辑文本。(当然,物理键盘只能在我的应用程序开发过程中使用,不会出现在成品中)。我希望我说得够清楚,但如有需要,我会乐意提供更多详情。
如果能在这两个问题上提供任何帮助,我们将不胜感激。
干杯。
编辑:我遇到的一些有用的链接:
根据QObject调整qt虚拟化键盘的大小
发布于 2020-10-22 09:15:18
好的,经过几天与虚拟键盘的战争,我终于达到了预期的效果。
在找到这宝石般的指南之后,由于包含QTableView和QtVirtualKeyboard的小部件是使用exec()方法显示的QDialog,这意味着窗口属性不允许键盘修改数据。虽然指南中提出的解决方案并没有解决我的问题,但是让我的小部件继承QWidget确实让我走上了正确工作的道路。
我这么说是因为一旦我将QDialog转换为QWidget,每次按下键时,都会出现控制台输出错误,表示unknown:0 input method is not set。
解决方案是从我的Qt:Dialog方法中移除setWindowFlags()标志。最重要的是,将我的QQuickWidget的焦点策略设置为NoFocus,如下所示:
// In constructor
QUrl source(QML_FILE_PATH + "virtualkeyboard.qml");
m_quickWidget->setSource(source);
m_quickWidget->setAttribute(Qt::WA_AcceptTouchEvents);
m_quickWidget->setFocusPolicy(Qt::NoFocus);
ui->verticalLayout->addWidget(m_quickWidget);哈利路亚!!我的QtVirtualKeyboard最终将点击的密钥发送到我的可编辑单元格。
最后,要打开键盘,只需单击我表中的一个单元格,我确信有一个更好的解决方案,但我将一个插槽连接到我的QTableView的pressed信号,并手动设置输入方法的可见性:
void ConfigWindow::on_tableView_pressed(const QModelIndex &index)
{
if ((index.column() == 0) || (index.column() == 1))
{
QApplication::inputMethod()->show();
}
}希望这能帮助那些有着和我一样的麻烦的人使用这个强大而又痛苦的、缺乏文档的插件。
https://stackoverflow.com/questions/64428819
复制相似问题