使用 PyQt5 创建上下文菜单
Create a context menu with PyQt5
我正在尝试创建一个上下文菜单,我看到了一些 tuts,他们在其中编写了下一个代码,例如:
import sys
from PyQt5.QtWidgets import QMainWindow, qApp, QMenu, QApplication
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Context menu')
self.show()
def contextMenuEvent(self, event):
cmenu = QMenu(self)
newAct = cmenu.addAction("New")
openAct = cmenu.addAction("Open")
quitAct = cmenu.addAction("Quit")
action = cmenu.exec_(self.mapToGlobal(event.pos()))
if action == quitAct:
qApp.quit()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我尝试在我的代码中实现它有 2 个模块但不起作用:
main.py
import win0 as w0
from PyQt5.QtWidgets import QMainWindow, QApplication, QMenu
class Main(QMainWindow, w0.Ui_win0):
win0 = None
__win0_ui = None
def __init__(self):
self.win0 = QMainWindow()
self.__win0_ui = w0.Ui_win0()
self.__win0_ui.setupUi(self.win0)
self.win0.show()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
main = Main()
app.setStyle("Fusion")
sys.exit(app.exec_())
win0.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTableWidget, QFrame, QMenu, QMainWindow, qApp
class Table(QTableWidget, QMainWindow):
def contextMenuEvent(self, event):
cmenu = QMenu(self)
newAct = cmenu.addAction("New")
openAct = cmenu.addAction("Open")
quitAct = cmenu.addAction("Quit")
action = cmenu.exec_(self.mapToGlobal(event.pos()))
if action == quitAct:
qApp.quit()
class Ui_win0(object):
def setupUi(self, win0):
win0.setObjectName("win0")
win0.resize(703, 757)
win0.setStyleSheet("background-color: rgba(29, 29, 29);"
"\ncolor: rgb(171, 171, 171);")
self.centralwidget = QtWidgets.QWidget(win0)
self.centralwidget.setObjectName("centralwidget")
self.table1 = Table(self.centralwidget)
# Code...
self.retranslateUi(win0)
QtCore.QMetaObject.connectSlotsByName(win0)
def retranslateUi(self, win0):
# Code...
我没有粘贴所有代码,因为不要让它太长,但我想在 Table class 中实现示例,因为我想扩展一些选项或其他选项如果光标在 table 列表上或 window.
的其他位置
我做错了什么?
编辑 1 ------------------------------------------ ------------------------------
为了更清楚,它是下一个想法
白色方框代表window
不同地方的右键菜单
我将提供一个“模板代码”,因为由于各种原因(错误的继承、不必要的对象层、不需要的功能和实现),所提供的几乎无法使用。
在以下示例中,我混合使用了两种可能的方法来创建菜单事件:
- 在子类上实现
contextMenuEvent()
customContextMenuRequested
没有子类化的信号连接
class Table(QtWidgets.QTableWidget):
def contextMenuEvent(self, event):
menu = QtWidgets.QMenu()
index = self.indexAt(event.pos())
someAction = menu.addAction('')
if index.isValid():
someAction.setText('Selected item: "{}"'.format(index.data()))
else:
someAction.setText('No selection')
someAction.setEnabled(False)
anotherAction = menu.addAction('Do something')
res = menu.exec_(event.globalPos())
if res == someAction:
print('first action triggered')
class Example(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
central = QtWidgets.QWidget()
self.setCentralWidget(central)
layout = QtWidgets.QHBoxLayout(central)
self.table = Table(10, 4)
layout.addWidget(self.table)
layout.addStretch()
central.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
central.customContextMenuRequested.connect(self.emptySpaceMenu)
def emptySpaceMenu(self):
menu = QtWidgets.QMenu()
menu.addAction('You clicked on the empty space')
menu.exec_(QtGui.QCursor.pos())
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
example = Example()
example.show()
sys.exit(app.exec_())
如您所见,您使用的大部分结构都被删除为不必要或多余的。这是一个非常重要的方面:虽然保持高度明确和“冗长”的对象结构通常可以实现更好的抽象,但过度使用只会让事情变得不必要地复杂和非常混乱,甚至对程序员来说也是如此。
我正在尝试创建一个上下文菜单,我看到了一些 tuts,他们在其中编写了下一个代码,例如:
import sys
from PyQt5.QtWidgets import QMainWindow, qApp, QMenu, QApplication
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Context menu')
self.show()
def contextMenuEvent(self, event):
cmenu = QMenu(self)
newAct = cmenu.addAction("New")
openAct = cmenu.addAction("Open")
quitAct = cmenu.addAction("Quit")
action = cmenu.exec_(self.mapToGlobal(event.pos()))
if action == quitAct:
qApp.quit()
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我尝试在我的代码中实现它有 2 个模块但不起作用:
main.py
import win0 as w0
from PyQt5.QtWidgets import QMainWindow, QApplication, QMenu
class Main(QMainWindow, w0.Ui_win0):
win0 = None
__win0_ui = None
def __init__(self):
self.win0 = QMainWindow()
self.__win0_ui = w0.Ui_win0()
self.__win0_ui.setupUi(self.win0)
self.win0.show()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
main = Main()
app.setStyle("Fusion")
sys.exit(app.exec_())
win0.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTableWidget, QFrame, QMenu, QMainWindow, qApp
class Table(QTableWidget, QMainWindow):
def contextMenuEvent(self, event):
cmenu = QMenu(self)
newAct = cmenu.addAction("New")
openAct = cmenu.addAction("Open")
quitAct = cmenu.addAction("Quit")
action = cmenu.exec_(self.mapToGlobal(event.pos()))
if action == quitAct:
qApp.quit()
class Ui_win0(object):
def setupUi(self, win0):
win0.setObjectName("win0")
win0.resize(703, 757)
win0.setStyleSheet("background-color: rgba(29, 29, 29);"
"\ncolor: rgb(171, 171, 171);")
self.centralwidget = QtWidgets.QWidget(win0)
self.centralwidget.setObjectName("centralwidget")
self.table1 = Table(self.centralwidget)
# Code...
self.retranslateUi(win0)
QtCore.QMetaObject.connectSlotsByName(win0)
def retranslateUi(self, win0):
# Code...
我没有粘贴所有代码,因为不要让它太长,但我想在 Table class 中实现示例,因为我想扩展一些选项或其他选项如果光标在 table 列表上或 window.
的其他位置我做错了什么?
编辑 1 ------------------------------------------ ------------------------------ 为了更清楚,它是下一个想法
白色方框代表window
不同地方的右键菜单我将提供一个“模板代码”,因为由于各种原因(错误的继承、不必要的对象层、不需要的功能和实现),所提供的几乎无法使用。
在以下示例中,我混合使用了两种可能的方法来创建菜单事件:
- 在子类上实现
contextMenuEvent()
customContextMenuRequested
没有子类化的信号连接
class Table(QtWidgets.QTableWidget):
def contextMenuEvent(self, event):
menu = QtWidgets.QMenu()
index = self.indexAt(event.pos())
someAction = menu.addAction('')
if index.isValid():
someAction.setText('Selected item: "{}"'.format(index.data()))
else:
someAction.setText('No selection')
someAction.setEnabled(False)
anotherAction = menu.addAction('Do something')
res = menu.exec_(event.globalPos())
if res == someAction:
print('first action triggered')
class Example(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
central = QtWidgets.QWidget()
self.setCentralWidget(central)
layout = QtWidgets.QHBoxLayout(central)
self.table = Table(10, 4)
layout.addWidget(self.table)
layout.addStretch()
central.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
central.customContextMenuRequested.connect(self.emptySpaceMenu)
def emptySpaceMenu(self):
menu = QtWidgets.QMenu()
menu.addAction('You clicked on the empty space')
menu.exec_(QtGui.QCursor.pos())
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
example = Example()
example.show()
sys.exit(app.exec_())
如您所见,您使用的大部分结构都被删除为不必要或多余的。这是一个非常重要的方面:虽然保持高度明确和“冗长”的对象结构通常可以实现更好的抽象,但过度使用只会让事情变得不必要地复杂和非常混乱,甚至对程序员来说也是如此。