QTermWidget:模拟按键Python/C++
QTermWidget: simulating a key press Python/C++
所以我试图在程序中向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++中尝试过这个吗?
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_())
相关文章:
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- Pybind11:将元组列表从Python传递到C++
- 如何在c++中使用引用实现类似python的行为
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 递归列出所有目录中的C++与Python与Ruby的性能
- IPC使用多个管道和分支进程来运行Python程序
- 从python中调用C++函数并获取返回值
- Python 3.7 和 excess_args 的 SWIG 问题
- Python中的for循环与C++有何不同
- 使用Pybind11向Python公开Eigen::张量
- Python str to C++ to Python str
- 如何使用Python从C++中读取谷物序列化数据
- 如何在C++中使用pybind11加载一个pickle python列表
- 如何在c++中使用system()来运行包含空格的python脚本
- python集合的C++等价物是什么.计数器
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 从python调用openMP共享库时,未定义opnMP函数
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- 如何将真正的字符串从python c-api转换为python脚本
- Python ctype 'c_char_p' Memory Leak