我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用PyQt5.QtWidgets.QShortcut()。
def setGraphView(self, graphView): self.graphView = graphView # Setup Layout layout = QtWidgets.QVBoxLayout(self) layout.addWidget(self.graphView) self.setLayout(layout) # Setup hotkeys for the following actions. deleteShortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Delete), self) deleteShortcut.activated.connect(self.graphView.deleteSelectedNodes) frameShortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_F), self) frameShortcut.activated.connect(self.graphView.frameSelectedNodes) frameShortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_A), self) frameShortcut.activated.connect(self.graphView.frameAllNodes)
def qui_key(self, key_name, key_combo, func): self.hotkey[key_name] = QtWidgets.QShortcut(QtGui.QKeySequence(key_combo), self) self.hotkey[key_name].activated.connect( func )
def Establish_Connections(self): super(self.__class__,self).Establish_Connections() # custom ui response # shortcut connection self.hotkey = {} # self.hotkey['my_key'] = QtWidgets.QShortcut(QtGui.QKeySequence( "Ctrl+1" ), self) # self.hotkey['my_key'].activated.connect(self.my_key_func) # ---- user response list ----
def setupUi(self): self.setObjectName("Dialog") self.resize(355, 76) self.buttonBox = QtWidgets.QDialogButtonBox(self) self.buttonBox.setGeometry(QtCore.QRect(0, 40, 341, 32)) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) font8 = QtGui.QFont('Courier New',8) self.buttonBox.addButton('Find (Enter)',QtWidgets.QDialogButtonBox.AcceptRole) self.findnext_Btn = self.buttonBox.addButton('Find next (F3)',QtWidgets.QDialogButtonBox.ApplyRole) self.buttonBox.addButton('Cancel',QtWidgets.QDialogButtonBox.RejectRole) self.buttonBox.setObjectName("buttonBox") self.lineEdit_search = QtWidgets.QLineEdit(self) self.lineEdit_search.setGeometry(QtCore.QRect(100, 10, 221, 20)) self.lineEdit_search.setObjectName("lineEdit_search") self.lineEdit_search.setFont(font8) self.lineEdit_search.setClearButtonEnabled(True) self.label = QtWidgets.QLabel(self) self.label.setGeometry(QtCore.QRect(10, 10, 71, 16)) self.label.setObjectName("label") self.lineEdit_search.setText('search here') self.retranslateUi() #button clicks events self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.findnext_Btn.clicked.connect(self.acceptNext) # shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_F3), self) # # shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_F3), self, QtCore.SLOT(self.acceptNext)) # self.setShortcutEnabled(shortcut.id(),True) # findNext_ = QtWidgets.QAction('&FindNext', self) # findNext_.setShortcut(QtGui.QKeySequence(QtCore.Qt.Key_F3)) # findNext_.triggered.connect(self.acceptNext) find_ = QtWidgets.QAction('&Find', self) find_.setShortcut(self.tr("Enter")) find_.triggered.connect(self.accept) QtCore.QMetaObject.connectSlotsByName(self) self.setTabOrder(self.lineEdit_search, self.findnext_Btn) self.setTabOrder(self.findnext_Btn, self.buttonBox) # def exec(self): # return 1
def __init__(self, window_width=1000, window_height=950): super().__init__() # Initialize Plain Text Widget self.text_widget = QtWidgets.QPlainTextEdit() # Default Mode to Insertion self.insert = True # EventFilter FocusOut self._filter = Filter() # Setup Shortcuts self.shift_tab = QtWidgets.QShortcut(QtGui.QKeySequence('Shift+Tab'), self) #self.shift_tab.activated.setFocus() # Default start up File self.file_name = 'Untitled.txt' self.file_path = ['/', ''] self.file_type = self.file_name.split('.')[-1] self.syntax = highlighter.PythonHighlighter(self.text_widget.document()) self.assign_syntax_def() # Initialize Menus self.menu_bar = self.menuBar() self.file_menu() self.edit_menu() self.format_menu() self.preferences_menu() self.finder_toolbar() # Set Default Pallete self.default_visual() # Initialize UI Related Properties self.notepad_ui() self.setCentralWidget(self.text_widget) self.setWindowIcon(QtGui.QIcon('assets/icons/notepad.png')) self.resize(window_width, window_height) # Center the main window to the screen self.center() self.show()
def __init__(self): super().__init__() vbox = QtWidgets.QVBoxLayout() self.setLayout(vbox) # self.setMinimumWidth(180) self.updating_gui_bool = False self.list_widget = QtWidgets.QListWidget() # self.list_widget.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) vbox.addWidget(self.list_widget) self.list_widget.itemSelectionChanged.connect(self.on_selection_changed) hbox = QtWidgets.QHBoxLayout() vbox.addLayout(hbox) self.add_to_list_qle = QtWidgets.QLineEdit() hbox.addWidget(self.add_to_list_qle) self.add_to_list_qle.setPlaceholderText("New item") QtWidgets.QShortcut( QtGui.QKeySequence(QtCore.Qt.Key_Return), self.add_to_list_qle, member=self.add_new_phrase_button_clicked, context=QtCore.Qt.WidgetShortcut ) self.add_new_phrase_qpb = QtWidgets.QPushButton("Add") self.add_new_phrase_qpb.clicked.connect(self.add_new_phrase_button_clicked) hbox.addWidget(self.add_new_phrase_qpb) hbox = QtWidgets.QHBoxLayout() vbox.addLayout(hbox) self.edit_texts_qpb = QtWidgets.QPushButton() hbox.addWidget(self.edit_texts_qpb) self.edit_texts_qpb.setIcon(QtGui.QIcon(mc.mc_global.get_icon_path("pencil-2x.png"))) self.edit_texts_qpb.clicked.connect(self.on_edit_texts_clicked) self.move_to_top_qpb = QtWidgets.QPushButton() hbox.addWidget(self.move_to_top_qpb) self.move_to_top_qpb.setIcon(QtGui.QIcon(mc.mc_global.get_icon_path("data-transfer-upload-2x.png"))) self.move_to_top_qpb.clicked.connect(self.on_move_to_top_clicked) self.move_up_qpb = QtWidgets.QPushButton() hbox.addWidget(self.move_up_qpb) self.move_up_qpb.setIcon(QtGui.QIcon(mc.mc_global.get_icon_path("arrow-top-2x.png"))) self.move_up_qpb.clicked.connect(self.on_move_up_clicked) self.move_down_qpb = QtWidgets.QPushButton() hbox.addWidget(self.move_down_qpb) self.move_down_qpb.setIcon(QtGui.QIcon(mc.mc_global.get_icon_path("arrow-bottom-2x.png"))) self.move_down_qpb.clicked.connect(self.on_move_down_clicked) hbox.addStretch(1) self.delete_phrase_qpb = QtWidgets.QPushButton() hbox.addWidget(self.delete_phrase_qpb) self.delete_phrase_qpb.setIcon(QtGui.QIcon(mc.mc_global.get_icon_path("trash-2x.png"))) self.delete_phrase_qpb.clicked.connect(self.on_delete_clicked) self.update_gui() self.list_widget.setCurrentRow(0) # -the first row
def _setup_hotkeys(self, action_map): shortcuts = {} def resolve_ambiguity(keys): widget = QtWidgets.QApplication.focusWidget() while widget: if widget == self.audio: shortcuts[keys].activated.emit() widget = widget.parent() for context, items in self._api.opt.hotkeys.items(): for item in items: keys, cmd_name, *cmd_args = item action = action_map.get((cmd_name, *cmd_args)) if action and context == 'global': # add shortcut hint text to the buttons in the menu action.setShortcut(QtGui.QKeySequence(keys)) # prevent the action from triggering any signal on pressing # its shortcut. (the action is parent of the main window, # and the main window widget can never have focus by # itself.) # basically, all hotkey triggers are to be handled by the # QShortcut below. action.setShortcutContext(QtCore.Qt.WidgetShortcut) shortcut = QtWidgets.QShortcut(QtGui.QKeySequence(keys), self) shortcuts[keys] = shortcut shortcut.setContext(QtCore.Qt.WidgetWithChildrenShortcut) shortcut.activated.connect(functools.partial( self._api.cmd.run, self._api.cmd.get(cmd_name, cmd_args))) if context == 'audio': shortcut.setParent(self.audio) elif context != 'global': raise RuntimeError('Invalid shortcut context') # when the user focuses some widget X or its child, and presses # a hotkey that's defined both for widget X and for the window, # qt doesn't know whether the user meant to use global hotkey # or the widget X hotkey since the focused widget is a # descendant of both the widget X and the main window. # # in case a shortcut is ambiguous, qt invokes the ambiguous # signals in turns each time user presses given shortcut. if context == 'global': # since ambiguous keypress happens only if user has # actually focused an ambiguous widget X, in case when it # is the main window that receives ambiguous trigger, the # main window must pass the event to the widget X. shortcut.activatedAmbiguously.connect( functools.partial(resolve_ambiguity, keys)) else: # in case where it is the ambiguous widget X that receives # ambiguous trigger, it can just carry on with execution. shortcut.activatedAmbiguously.connect( shortcut.activated.emit)