Chirag Dhyani
Chirag Dhyani

Reputation: 1063

PyQT4: How to get context menu set for qListView

I have a QlistView which gets populated with content. I want to know method to add contextmenu(Right click) to show options like "add" or "remove'. I have tried various methods, most of the straigt fwd ones are for Qlist Widget. As, the arch. is MVC, I have to go by QlistView().

I have tried following but it do not work:

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))

But the above wont work. Thanks in advance for your time and help.

Upvotes: 2

Views: 1775

Answers (2)

TheLazyScripter
TheLazyScripter

Reputation: 2665

I am going to assume that you are using this for the "Off_Strip" program you were trying to get help on earlier so I went ahead and added the revised code for adding a Custom Context Menu to said code! The revised code will be commented!

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()

All the rest of the code was left the same

Upvotes: 1

lskrinjar
lskrinjar

Reputation: 5793

Add this to setupUi() method:

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

you should have defined your qListView as object attribute like:

self.view = QListView()

Upvotes: 2

Related Questions