7

To keep the GUI widgets number to minimum I need to find a way to give to user a choice of pull-down menu items that could be used to filter out the displayed in a listWidget items. Let's say the listWidget lists 5 different categories of Items: "Cat A", "Cat B","Cat C","Cat D","Cat E". I could implement the radio or checkboxes for each item category. But then 5 radio buttons or checkboxes would take a lot of GUI space. A combobox with the checkable items seems to be a right choice. Any ideas?

from PyQt4 import QtGui, QtCore
import sys, os


class CheckableComboBox(QtGui.QComboBox):
    def __init__(self):    
        super(CheckableComboBox, self).__init__()

    def flags(self, index):
        return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled


class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

        self.ComboBox = CheckableComboBox()
        for i in range(3):
            self.ComboBox.addItem("Combobox Item " + str(i))

        myBoxLayout.addWidget(self.ComboBox)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
alphanumeric
  • 17,967
  • 64
  • 244
  • 392

3 Answers3

14

This idea of a multi-select combo has come up before, but I'm not sure that its the best solution. Really, all that's needed is a tool-button with a drop-down menu (similar to the history buttons in a web-browser).

Here's a basic demo that illustrates both options (button left, combo right):

screenshot screenshot

PyQt5:

from PyQt5 import QtWidgets, QtGui, QtCore

class CheckableComboBox(QtWidgets.QComboBox):
    def __init__(self):
        super(CheckableComboBox, self).__init__()
        self.view().pressed.connect(self.handleItemPressed)
        self.setModel(QtGui.QStandardItemModel(self))

    def handleItemPressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.checkState() == QtCore.Qt.Checked:
            item.setCheckState(QtCore.Qt.Unchecked)
        else:
            item.setCheckState(QtCore.Qt.Checked)

class Dialog_01(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        myQWidget = QtWidgets.QWidget()
        myBoxLayout = QtWidgets.QHBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.ComboBox = CheckableComboBox()
        self.toolbutton = QtWidgets.QToolButton(self)
        self.toolbutton.setText('Categories ')
        self.toolmenu = QtWidgets.QMenu(self)
        for i in range(3):
            self.ComboBox.addItem('Category %s' % i)
            item = self.ComboBox.model().item(i, 0)
            item.setCheckState(QtCore.Qt.Unchecked)
            action = self.toolmenu.addAction('Category %s' % i)
            action.setCheckable(True)
        self.toolbutton.setMenu(self.toolmenu)
        self.toolbutton.setPopupMode(QtWidgets.QToolButton.InstantPopup)
        myBoxLayout.addWidget(self.toolbutton)
        myBoxLayout.addWidget(self.ComboBox)

if __name__ == '__main__':

    app = QtWidgets.QApplication(['Test'])
    dialog_1 = Dialog_01()
    dialog_1.show()
    app.exec_()
    
**PyQt4**:

from PyQt4 import QtGui, QtCore

class CheckableComboBox(QtGui.QComboBox):
    def __init__(self):
        super(CheckableComboBox, self).__init__()
        self.view().pressed.connect(self.handleItemPressed)
        self.setModel(QtGui.QStandardItemModel(self))

    def handleItemPressed(self, index):
        item = self.model().itemFromIndex(index)
        if item.checkState() == QtCore.Qt.Checked:
            item.setCheckState(QtCore.Qt.Unchecked)
        else:
            item.setCheckState(QtCore.Qt.Checked)

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow, self).__init__()
        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QHBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.ComboBox = CheckableComboBox()
        self.toolbutton = QtGui.QToolButton(self)
        self.toolbutton.setText('Categories ')
        self.toolmenu = QtGui.QMenu(self)
        self.toolbutton.setMenu(self.toolmenu)
        self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup)
        for i in range(3):
            self.ComboBox.addItem('Category %s' % i)
            item = self.ComboBox.model().item(i, 0)
            item.setCheckState(QtCore.Qt.Unchecked)
            action = self.toolmenu.addAction('Category %s' % i)
            action.setCheckable(True)
        myBoxLayout.addWidget(self.toolbutton)
        myBoxLayout.addWidget(self.ComboBox)

if __name__ == '__main__':

    app = QtGui.QApplication(['Test'])
    dialog_1 = Dialog_01()
    dialog_1.show()
    app.exec_()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • This looks very the same as I've envisioned it. Thanks Ekhumoro! – alphanumeric Apr 01 '14 at 04:02
  • If I use self.connect(action, QtCore.SIGNAL("triggered()"), self.myMethod) there are no args sent. It would make sense to submit a toolmenu's action as an arg to myMethod(self, arg). Would you mind showing how each action could be hooked to a method that would be used to receive an action as an arg (so an action's "checked" status could be verified (along with its name)? ("action" here is one of the instance name: result of action = self.toolmenu.addAction("Category " + str(i)) – alphanumeric Apr 01 '14 at 06:23
  • @Sputnix. Just connect to the [triggered](http://qt-project.org/doc/qt-4.8/qmenu.html#triggered) signal of the tool-menu - it sends the action which was triggered. Also, you can iterate over the actions of the menu using its [actions](http://qt-project.org/doc/qt-4.8/qwidget.html#actions) method. – ekhumoro Apr 01 '14 at 18:02
  • Thanks! Almost there! – alphanumeric Apr 01 '14 at 18:46
  • If no .addAction() actions were added to QMenu then clicking on QToolButton brings a Warining: void QCocoaWindow::syncWindowState(Qt::WindowState) invalid window content view size, check your window geometry I wonder if it is a .setPopupMode method that triggers it? Should be QtGui.QToolButton "unset" from .setPopupMode(QtGui.QToolButton.InstantPopup) if there are no actions connected? – alphanumeric Apr 01 '14 at 19:04
  • @Sputnix. Not sure, because I can't test on OSX (I don't get any errors on Linux). Maybe you should only set the menu on the button once you have actions to add to it. – ekhumoro Apr 01 '14 at 19:14
  • Thanks! If I figure this out I'll let you know. Otherwise it works great! Thanks again! – alphanumeric Apr 01 '14 at 21:56
1

It is very easy. Just set the item Checkable using the flags() function in the model associated with the comboBox.

def flags(self, index):
    return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled

UPDATE


This may not be the best method, But it should sort your problem. Here is a something u may want look at , IF you have space restrictionn and cannot display the complete listView, Just resize it until it looks like a ComboBox.

enter image description here

thecreator232
  • 2,145
  • 1
  • 36
  • 51
-1

(Not an answere to the question, hence I used most of the code from here)

I added a function and changed the name to RadioComboBox if anyone else wants to have a class for a RadioComboBox.

from PyQt4 import QtCore
from PyQt4.QtGui import QComboBox, QStandardItemModel


class RadioComboBox(QComboBox):
    def __init__(self):
        super(RadioComboBox, self).__init__()
        self.view().pressed.connect(self.handle_item_pressed)
        self.setModel(QStandardItemModel(self))

    def handle_item_pressed(self, index):
        item = self.model().itemFromIndex(index)
        target_row = item.index().row()
        if item.checkState() != QtCore.Qt.Checked:
            item.setCheckState(QtCore.Qt.Checked)
        self.check_others(target_row)

    def check_others(self, target_row):
        for i in range(self.model().rowCount()):
            if i == target_row:
                continue
            else:
                item = self.model().item(i)
                item.setCheckState(QtCore.Qt.Unchecked)
axel_ande
  • 359
  • 1
  • 4
  • 20