QTMWIDGET:模拟按键Python/C++

2024-05-23 17:40:02 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试图在我的程序中向QTermWidget发送一个模拟的返回键。我在Python /Qt4和C++ +QT4中都有这个版本。在这一点上,我真的不关心它是否是用两种语言写的,现在我对C++语法有了一个相当好的理解。如果说这两种语言的答案都是天赐的。在

到目前为止我已经试过了

   QTest.KeyClick(self.Terminal, Qt.KeyReturn, Qt.NoModifier) // syntax is python here

以及

^{pr2}$

还有一些我现在记不清了。在

谢谢你的帮助。在


Tags: 答案self程序版本语言语法qtqt4
2条回答
你在C++中尝试过这个吗?在

QKeyEvent *ev = new QKeyEvent( QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier );
myApp->postEvent( receivingWidget, ev );

这对我有用,也应该和其他小部件一起工作。在

import sys
from PyQt4 import QtCore, QtGui, Qt
from ChildWindow import ChildWindow

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        QtCore.QTimer.singleShot(500, self.showChildWindow)
        self.textEdit = QtGui.QTextEdit(self)
        self.textEdit.resize(100, 100)

    def showChildWindow(self):
        QtGui.QApplication.sendEvent(self.textEdit, QtGui.QKeyEvent(QtGui.QKeyEvent.KeyPress, QtCore.Qt.Key_A, QtCore.Qt.NoModifier, "hello"))
        QtGui.QApplication.sendEvent(self.textEdit, QtGui.QKeyEvent(QtGui.QKeyEvent.KeyPress, QtCore.Qt.Key_Return, QtCore.Qt.NoModifier))


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MainWindow()
    myapp.show()
    sys.exit(app.exec_())

相关问题 更多 >