【问题标题】:QTermWidget: simulating a key press Python/C++QTermWidget:模拟按键 Python/C++
【发布时间】:2013-02-07 15:22:47
【问题描述】:

所以我试图在我的程序中向 QTermWidget 发送一个模拟的返回键。我在 Python /Qt4 和 C++/Qt4 中都有这个的工作版本。在这一点上,我真的不在乎它是否是用任何一种语言编写的,因为我对 C++ 语法有了不错的掌握。话虽这么说,任何一种语言的答案都是天赐之物。

到目前为止我已经尝试过

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

    key_press = QKeyEvent(QEvent.KeyPress, Qt.Key_Return, Qt.NoModifier)
    self.Terminal.keyPressEvent(key_press)
    key_release = QKeyEvent(QEvent.KeyRelease, Qt.Key_Return, Qt.NoModifier)
    self.Terminal.keyPressEvent(key_release)

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

感谢您的帮助。

【问题讨论】:

    标签: c++ python qt terminal


    【解决方案1】:

    您是否在 C++ 中尝试过这个?

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

    【讨论】:

      【解决方案2】:

      这对我有用,也应该与其他小部件一起使用。

      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_())
      

      【讨论】:

        猜你喜欢
        • 2020-04-29
        • 2011-03-04
        • 2011-11-10
        • 2012-02-15
        • 2015-04-13
        • 2021-03-07
        • 2012-01-30
        • 1970-01-01
        • 2011-03-23
        相关资源
        最近更新 更多