[英]About pyqt5 manubar hotkey
I try myself to write a shortcut sample about the menubar, but I got some issue about it. 我尝试自己编写有关菜单栏的快捷方式示例,但是我遇到了一些问题。 I have a
File(&F)
on the menubar, and in the File(&F)
there is a Save File (Ctrl + S) item, I want to know that why I can't use the shortcut with Ctrl + S
after I press Alt + F
我在菜单栏上有一个
File(&F)
,并且在File(&F)
有一个Save File(Ctrl + S)项目,我想知道为什么为什么我按下后无法在Ctrl + S
使用快捷方式Alt + F
menuBar = self.menuBar()
fileMenu = menuBar.addMenu("&File")
self.fileMenu .addAction(self.SaveFileBt)
self.SaveFileBt.setShortcut("Ctrl+S")
In the example below, you have three ways to invoke the save
action . 在下面的示例中,您可以通过三种方式调用
save
操作 。
Press the corresponding menu items as shown in the image. 如图所示,按相应的菜单项。
Use the keyboard shortcut Ctrl+S
使用键盘快捷键
Ctrl+S
Use the keyboard shortcut Alt+F+S
使用键盘快捷键
Alt+F+S
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
menuBar = self.menuBar()
fileMenu = menuBar.addMenu("&File")
fileMenu.addAction("New")
# ---> & <---
save = QAction("&Save", self)
save.setShortcut("Ctrl+S")
fileMenu.addAction(save)
edit = fileMenu.addMenu("Edit")
edit.addAction("copy")
edit.addAction("paste")
quit = QAction(QIcon("D:/_Qt/__Qt/img/exit.png"), "Quit",self)
quit.setShortcut('Ctrl+Q')
quit.triggered.connect(qApp.quit)
fileMenu.addAction(quit)
fileMenu.triggered[QAction].connect(self.processtrigger)
def processtrigger(self, q):
print( "{} is triggered".format(q.text()))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.setWindowTitle("Qmenu")
ex.resize(350,300)
ex.show()
sys.exit(app.exec_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.