我在嵌入式设备上使用qt小部件,并且在虚拟键盘上有问题。键盘显示为全屏,并与所有应用程序重叠。
文章介绍了Virtual keyboard top black screen in Yocto如何解决这一问题。
简而言之,您需要找到带有键盘的QQuickWindow,并在此窗口上调用setMask。然后键盘上方的区域将是透明的。
我有问题,如何找到QQuickWindow与虚拟键盘。我试着用
QApplication::allWidgets()但窗户不在这里。
发布于 2020-09-18 22:56:36
要获得所有的窗口,您可以使用QGuiApplication::allWindows(),但这还不够,因为QtVirtualKeyboard窗口不一定是在开始时创建的,因此必须使用QInputMethod的visibleChanged信号。我没有使用来自QQuickWindow的信息进行筛选,因为通常应用程序可以有其他的信息,而是使用窗口所属的类的名称。
#include <QApplication>
#include <QWindow>
#include <cstring>
static void handleVisibleChanged(){
if (!QGuiApplication::inputMethod()->isVisible())
return;
for(QWindow * w: QGuiApplication::allWindows()){
if(std::strcmp(w->metaObject()->className(), "QtVirtualKeyboard::InputView") == 0){
if(QObject *keyboard = w->findChild<QObject *>("keyboard")){
QRect r = w->geometry();
r.moveTop(keyboard->property("y").toDouble());
w->setMask(r);
return;
}
}
}
}
int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
QApplication a(argc, argv);
QObject::connect(QGuiApplication::inputMethod(), &QInputMethod::visibleChanged, &handleVisibleChanged);
// ...Python版本:
import os
import sys
from PySide2 import QtCore, QtGui, QtWidgets
# from PyQt5 import QtCore, QtGui, QtWidgets
def handleVisibleChanged():
if not QtGui.QGuiApplication.inputMethod().isVisible():
return
for w in QtGui.QGuiApplication.allWindows():
if w.metaObject().className() == "QtVirtualKeyboard::InputView":
keyboard = w.findChild(QtCore.QObject, "keyboard")
if keyboard is not None:
r = w.geometry()
r.moveTop(keyboard.property("y"))
w.setMask(QtGui.QRegion(r))
return
def main():
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
app = QtWidgets.QApplication(sys.argv)
QtGui.QGuiApplication.inputMethod().visibleChanged.connect(handleVisibleChanged)
w = QtWidgets.QLineEdit()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()发布于 2021-04-19 16:41:44
我有一个解决方案的人谁使用覆盆子与PyQt5。要完成eyllanesc的答案,因为Python版本不适用于PyQt5,实际上,我们遇到了以下问题:
TypeError: setMask(self,QRegion):参数1有意想不到的'QRect‘类型
要解决这个问题:
def handleVisibleChanged():
if not QtGui.QGuiApplication.inputMethod().isVisible():
return
for w in QtGui.QGuiApplication.allWindows():
if w.metaObject().className() == "QtVirtualKeyboard::InputView":
keyboard = w.findChild(QtCore.QObject, "keyboard")
if keyboard is not None:
region = w.mask()
rect = [w.geometry()]
rect[0].moveTop(keyboard.property("y"))
region.setRects(rect)
w.setMask(region)
return发布于 2020-09-18 14:36:09
您可以将findChildren与继承QObject的任何类(如QApplication )一起使用。例如,在main.cpp中
QApplication a(argc, argv);
QList<QQuickWindow *> wind = a.findChildren<QQuickWindow *>();这将给出指向应用程序中所有QQuickWindow的指针列表。
https://stackoverflow.com/questions/63955568
复制相似问题