How to create a combobox of checkboxes ?
-
Hello,
I want to create a combobox of checkboxes, which means that my combobox contains elements that are checkable.
I tried this method but it doesn't work.
from PyQt4 import QtGui, QtCore import sys, os 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)
self.imploc_extension = CheckableComboBox() for i in range(4): self.imploc_extension.addItem("csv") self.imploc_extension.addItem("xls") self.imploc_extension.addItem("mtx") self.imploc_extension.addItem("txt") item = self.imploc_extension.model().item(i, 0) item.setCheckState(QtCore.Qt.Unchecked)
Could you help me ?
Thanks in advance.
Best regards
-
Hello,
I want to create a combobox of checkboxes, which means that my combobox contains elements that are checkable.
I tried this method but it doesn't work.
from PyQt4 import QtGui, QtCore import sys, os 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)
self.imploc_extension = CheckableComboBox() for i in range(4): self.imploc_extension.addItem("csv") self.imploc_extension.addItem("xls") self.imploc_extension.addItem("mtx") self.imploc_extension.addItem("txt") item = self.imploc_extension.model().item(i, 0) item.setCheckState(QtCore.Qt.Unchecked)
Could you help me ?
Thanks in advance.
Best regards
@EJWA Why do you want to do this? A combo box is usually used to select one item.
For multi selection you should use list box. -
@jsulm said in How to create a combobox of checkboxes ?:
selection you should use list box.
I want to select multi items in a list, I will try with list box and see .
Thank you very much :)
-
Just a question, I want a combobox because it doesn't take a lot of interface in the window.
It is possible to do a combobox of checkboxes ? -
Just a question, I want a combobox because it doesn't take a lot of interface in the window.
It is possible to do a combobox of checkboxes ?@EJWA
But you need to understand that a combo box is only for selection of one item, do you want one item or multiple items to be selectable?If it's multiple items but you don't like how much space a listbox takes up, you must make the listbox height smaller.
-
I tried this example found in internet and it works :
from PyQt4 import QtGui, QtCore import sys, os 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.QVBoxLayout() myQWidget.setLayout(myBoxLayout) self.setCentralWidget(myQWidget) self.ComboBox = CheckableComboBox() for i in range(3): self.ComboBox.addItem("Combobox Item " + str(i)) item = self.ComboBox.model().item(i, 0) item.setCheckState(QtCore.Qt.Unchecked) self.toolbutton = QtGui.QToolButton(self) self.toolbutton.setText('Select Categories ') self.toolmenu = QtGui.QMenu(self) for i in range(3): action = self.toolmenu.addAction("Category " + str(i)) action.setCheckable(True) self.toolbutton.setMenu(self.toolmenu) self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup) myBoxLayout.addWidget(self.toolbutton) 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_())
-
I tried this example found in internet and it works :
from PyQt4 import QtGui, QtCore import sys, os 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.QVBoxLayout() myQWidget.setLayout(myBoxLayout) self.setCentralWidget(myQWidget) self.ComboBox = CheckableComboBox() for i in range(3): self.ComboBox.addItem("Combobox Item " + str(i)) item = self.ComboBox.model().item(i, 0) item.setCheckState(QtCore.Qt.Unchecked) self.toolbutton = QtGui.QToolButton(self) self.toolbutton.setText('Select Categories ') self.toolmenu = QtGui.QMenu(self) for i in range(3): action = self.toolmenu.addAction("Category " + str(i)) action.setCheckable(True) self.toolbutton.setMenu(self.toolmenu) self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup) myBoxLayout.addWidget(self.toolbutton) 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_())
@EJWA A list box does not have to be big: it will have a scroll bar if not all entries can be shown at the same time. Adding checkboxes to a combo box could irritate users as it a combo box is there to select one item from n, not m items from n.
-
Hello again,
If I understand there is no solution to do a combobox with checkboxes even like this example :
from PyQt4 import QtGui, QtCore import sys, os 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.QVBoxLayout() myQWidget.setLayout(myBoxLayout) self.setCentralWidget(myQWidget) self.ComboBox = CheckableComboBox() for i in range(3): self.ComboBox.addItem("Combobox Item " + str(i)) item = self.ComboBox.model().item(i, 0) item.setCheckState(QtCore.Qt.Unchecked) self.toolbutton = QtGui.QToolButton(self) self.toolbutton.setText('Select Categories ') self.toolmenu = QtGui.QMenu(self) for i in range(3): action = self.toolmenu.addAction("Category " + str(i)) action.setCheckable(True) self.toolbutton.setMenu(self.toolmenu) self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup) myBoxLayout.addWidget(self.toolbutton) 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_())
-
Hello again,
If I understand there is no solution to do a combobox with checkboxes even like this example :
from PyQt4 import QtGui, QtCore import sys, os 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.QVBoxLayout() myQWidget.setLayout(myBoxLayout) self.setCentralWidget(myQWidget) self.ComboBox = CheckableComboBox() for i in range(3): self.ComboBox.addItem("Combobox Item " + str(i)) item = self.ComboBox.model().item(i, 0) item.setCheckState(QtCore.Qt.Unchecked) self.toolbutton = QtGui.QToolButton(self) self.toolbutton.setText('Select Categories ') self.toolmenu = QtGui.QMenu(self) for i in range(3): action = self.toolmenu.addAction("Category " + str(i)) action.setCheckable(True) self.toolbutton.setMenu(self.toolmenu) self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup) myBoxLayout.addWidget(self.toolbutton) 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_())
@EJWA
The main issue is that it will close when you Check one item.
If you hax that, you then have the issue of how to close it.So overall as @jsulm says, its not a very good idea as
it breaks the standard for the operating system.Unless you mean, you just want user to check one and then its fine it closes.
-
So, it means that there is no solution other than using a list widget which can be open when you select multiple elements ?
-
So, it means that there is no solution other than using a list widget which can be open when you select multiple elements ?
@EJWA
well you can hax it if you really feel like itfrom PyQt4 import QtCore, QtGui app = QtGui.QApplication([]) class Combo(QtGui.QComboBox): def __init__(self, *args, **kwargs): super(Combo, self).__init__() self.addItems(['Item_1','Item_2','Item_3','Item_4','Item_5']) self.show() def hidePopup (self): pass inst=Combo() sys.exit(app.exec_())
https://stackoverflow.com/questions/25733891/how-to-cause-qcombobox-pull-down-menu-to-stay-open
-
Thanks very much. What about the tool button ? It is a good idea ?
self.toolbutton = QtGui.QToolButton(self) self.toolbutton.setText('Select Categories ') self.toolmenu = QtGui.QMenu(self) for i in range(3): action = self.toolmenu.addAction("Category " + str(i)) action.setCheckable(True) self.toolbutton.setMenu(self.toolmenu) self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup)
-
Thanks very much. What about the tool button ? It is a good idea ?
self.toolbutton = QtGui.QToolButton(self) self.toolbutton.setText('Select Categories ') self.toolmenu = QtGui.QMenu(self) for i in range(3): action = self.toolmenu.addAction("Category " + str(i)) action.setCheckable(True) self.toolbutton.setMenu(self.toolmenu) self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup)
@EJWA
You mean to use toolbutton to popup menu ?
or toolbutton inside combo box ?Using a popup menu might suffer from same issue to close when one is selected/checked.
-
the same functions as using a combobox with checkboxes
-
the same functions as using a combobox with checkboxes
@EJWA
Both ways can work even if not usual function.
Also if its rare user need to select multiple Categories its maybe ok.
-
Can I keep the toolbutton menu open when selecting multiple categories ?
-
Can I keep the toolbutton menu open when selecting multiple categories ?
@EJWA
well yes but its more haxy than combo box
https://stackoverflow.com/questions/2050462/prevent-a-qmenu-from-closing-when-one-of-its-qaction-is-triggered/11571550#11571550
you might need to use event filterWhy dont u just use a dialog ?
Click button.
Dialog opens with ListView,
use checks one or more Categories.
User press ok to close. -
Hello, it seems that this code doesn't work because I design the interface with QT creator, so I must generate the py file from the ui using the pyuic4 and then modify the py file. I am trying now this example and I
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) self.imploc_extension = CheckableComboBox() for i in range(4): self.imploc_extension.addItem("csv") self.imploc_extension.addItem("xls") self.imploc_extension.addItem("mtx") self.imploc_extension.addItem("txt") item = self.imploc_extension.model().item(i, 0) item.setCheckState(QtCore.Qt.Unchecked)
-
Hello, it seems that this code doesn't work because I design the interface with QT creator, so I must generate the py file from the ui using the pyuic4 and then modify the py file. I am trying now this example and I
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) self.imploc_extension = CheckableComboBox() for i in range(4): self.imploc_extension.addItem("csv") self.imploc_extension.addItem("xls") self.imploc_extension.addItem("mtx") self.imploc_extension.addItem("txt") item = self.imploc_extension.model().item(i, 0) item.setCheckState(QtCore.Qt.Unchecked)
@EJWA Modifying generated code isn't a good idea as your modifications will be lost next time the code is generated...
-
Finally,
I will use listwidget, it us easier. Thank you for all your help.