import sys
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
from PyQt4.QtTest import QTest
# Create a sample widget
def __init__(self):
super().__init__()
# Clicking the mouse on the widget will set the test keyClick, to make sure that the shortcut works.
def mousePressEvent(self, *a, **k):
QTest.keyClick(self, Qt.Key_C, Qt.ControlModifier)
super().mousePressEvent(*a, **k)
# Slot to receive the keyClick signal
def slot():
print('triggered')
if __name__ == '__main__':
w = Widget()
w.show()
act.setShortcutContext(Qt.WidgetWithChildrenShortcut)
w.addAction(act)
w.setFocusPolicy(Qt.StrongFocus)
act.triggered.connect(slot)
# This does nothing, but it should call slot().
QTest.keyClick(w, Qt.Key_C, Qt.ControlModifier)
# After this, the widget shows and clicking on it will trigger call the test.
sys.exit(app.exec_())
import sys
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
from PyQt4.QtTest import QTest
# Create a sample widget
class Widget(QtGui.QWidget):
def __init__(self):
super().__init__()
# Clicking the mouse on the widget will set the test keyClick, to make sure that the shortcut works.
def mousePressEvent(self, *a, **k):
QTest.keyClick(self, Qt.Key_C, Qt.ControlModifier)
super().mousePressEvent(*a, **k)
# Slot to receive the keyClick signal
def slot():
print('triggered')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
act = QtGui.QAction('test', w)
act.setShortcut(QtGui.QKeySequence('Ctrl+C'))
act.setShortcutContext(Qt.WidgetWithChildrenShortcut)
w.addAction(act)
w.setFocusPolicy(Qt.StrongFocus)
act.triggered.connect(slot)
# This does nothing, but it should call slot().
QTest.keyClick(w, Qt.Key_C, Qt.ControlModifier)
# After this, the widget shows and clicking on it will trigger call the test.
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks