PyQT4:如何为qListView设置上下文菜单

时间:2016-04-10 11:37:45

标签: python pyqt pyqt4

我有一个QlistView,里面填充了内容。我想知道添加上下文菜单的方法(右键单击)来显示“添加”或“删除”等选项。我尝试了各种方法,大多数方法都是针对Qlist Widget。因为,拱门是MVC,我必须通过QlistView()。

我试过以下但是不起作用:

def setupUi(self):
    QtCore.Qt.view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
    QtCore.Qt.view.connect(QtCore.Qt.view, QtCore.SIGNAL("customContextMenuRequested(QPoint)", self.onContext))

def onContext(self):
    # Create a menu
    menu = QtGui.QMenu("Menu", self)
    menu.addAction(self.mAction1)
    menu.addAction(self.mAction2)
    # Show the context menu.
    menu.exec_(QtCore.Qt.view.mapToGlobal(point))

但以上不会奏效。在此先感谢您的时间和帮助。

2 个答案:

答案 0 :(得分:1)

将此添加到setupUi()方法:

self.view.customContextMenuRequested.connect(self.onContext)

您应该将qListView定义为对象属性,如:

self.view = QListView()

答案 1 :(得分:0)

我将假设您正在使用这个用于之前尝试获得帮助的“Off_Strip”程序,因此我继续添加修改后的代码,以便为所述代码添加自定义上下文菜单!修改后的代码将被评论!

class Ui_Form(object):
    def setupUi(self, Form):
        self.Form = Form #Created a referance to the form object and edited all code to reflect this
        self.Form.setObjectName(_fromUtf8("Form"))
        self.Form.resize(518, 349)
        self.Form.setContextMenuPolicy(Qtore.Qt.CustomContextMenu) ################## Self explanitory
        self.Form.customContextMenuRequested.connect(self.openMenu) #################### again...
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(40, 40, 141, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(400, 330, 141, 16))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.horizontalLayoutWidget = QtGui.QWidget(Form)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(70, 200, 381, 31))
        self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.textEdit = QtGui.QTextEdit(self.horizontalLayoutWidget)
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.horizontalLayout.addWidget(self.textEdit)
        self.pushButton = QtGui.QPushButton(self.horizontalLayoutWidget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.horizontalLayout.addWidget(self.pushButton)
        self.pushButton_2 = QtGui.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(230, 270, 75, 23))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))

        self.retranslateUi(self.Form)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.file_open)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.check_start)
        QtCore.QMetaObject.connectSlotsByName(self.Form)

    def openMenu(self, position): ################### Code for custom menu, this is the default for a QUIT menu
        menu = QtGui.QMenu()
        quitAction = menu.addAction("Quit")
        action = menu.exec_(self.Form.mapToGlobal(position))
        if action == quitAction:
            QtGui.qApp.quit()

所有其余代码都保持不变