2

I'm trying to extend the QComboClass with a right click menu, and offer it an option to set the current index to -1 (clearing the selection). I'm having trouble invoking the context menu or even the right click event.

class ComboBox(QComboBox):
    def __init__(self, *args, **kwargs):
        super(ComboBox, self).__init__()
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.showMenu)

    def showMenu(self, pos):
        menu = QMenu()
        clear_action = menu.addAction("Clear Selection", self.clearSelection)
        action = menu.exec_(self.mapToGlobal(pos))

    def clearSelection(self):
        self.setCurrentIndex(-1)

Could someone tell me what I'm doing wrong?

3
  • Your example code works perfectly fine for me. What exactly is the problem? Maybe you should give a more complete example that shows how you are using the class.
    – ekhumoro
    Apr 14, 2016 at 15:15
  • I messed up how I used the class, I had another class from a deprecated module with the same name without this implemented. Which is why the code didn't work. Took a while to realize this! :\ Apr 19, 2016 at 5:43
  • 2
    I know this thread is about a year old, but you really should either choose the pyqt5 OR pyqt4 based on which you are using because slots and signal handling is treated differently between the two. May 1, 2017 at 2:47

2 Answers 2

4

can you try this,

def showMenu(self,event):
    menu = QMenu()
    clear_action = menu.addAction("Clear Selection", self)
    action = menu.exec_(self.mapToGlobal(event.pos()))
    if action == clear_action:
        self.clearSelection()
2

You can try this

import sys
from PyQt4 import QtGui
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QMenu



class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        self.lbl = QtGui.QLabel("Ubuntu", self)

        self.combo = QtGui.QComboBox(self)
        self.combo.setContextMenuPolicy(Qt.CustomContextMenu)
        self.combo.customContextMenuRequested.connect(self.showMenu)
        self.combo.addItem("Ubuntu")
        self.combo.addItem("Mandriva")
        self.combo.addItem("Fedora")
        self.combo.addItem("Red Hat")
        self.combo.addItem("Gentoo")

        self.combo.move(50, 50)
        self.lbl.move(50, 150)

        self.combo.activated[str].connect(self.onActivated)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QtGui.QComboBox')
        self.show()

    def showMenu(self,pos):
        menu = QMenu()
        clear_action = menu.addAction("Clear Selection")
        action = menu.exec_(self.mapToGlobal(pos))
        if action == clear_action:
            self.combo.setCurrentIndex(0)

    def onActivated(self, text):

        self.lbl.setText(text)
        self.lbl.adjustSize()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
1
  • 1
    While this code may answer the question, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation. Apr 14, 2016 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.