mardi 6 octobre 2020

Multiple Selection in QComboBox

I have two windows as shown in the figure below (MainWindow, and Dialog). There is a button (Second Window) in the MainWindow by which I can switch to the "Dialog" window as well as create "QComboBox" through the "CheckableComboBox" class. The class code is supposed to provide a combo-box with multiple selections capability. It is highly appreciated if you let me know how to solve the following problems:

  1. Is there a way to add the "checkbox" options on the left side of combo-box items?

  2. How do I access to the combo-box items? Thank you so much in advance! enter image description here

             from PyQt5 import QtGui, QtCore, QtWidgets
             import sys, os
    
             from First import Ui_First_MainWindow
             from Second import Ui_Second_Dialog
    
    
             class CheckableComboBox(QtWidgets.QComboBox):                    
                 def addItem(self, item):
                     super(CheckableComboBox, self).addItem(item)
                     item = self.model().item(self.count()-1,0)
                     item.setFlags(QtCore.Qt.ItemIsUserCheckable |QtCore.Qt.ItemIsEnabled)                
                     item.setCheckState(QtCore.Qt.Unchecked)
    
                 def itemChecked(self, index):
                     item = self.model().item(i,0)
                     return item.checkState() == QtCore.Qt.Checked
    
    
    
             class MyApp(QtWidgets.QMainWindow, Ui_First_MainWindow):
                 def __init__(self):
                     QtWidgets.QMainWindow.__init__(self)
                     Ui_First_MainWindow.__init__(self)
                     self.setupUi(self)
                     self.First_pB.clicked.connect(self.Second_EntryForm)
    
    
                 def Second_EntryForm(self):
                     dialog = QtWidgets.QDialog()
                     dialog.ui = Ui_Second_Dialog()
                     dialog.ui.setupUi(dialog)
    
    
                     ComboBox = CheckableComboBox(dialog)
                     for i in range(6):
                         ComboBox.addItem("Combobox Item " + str(i))
    
    
                     dialog.exec_()
                     dialog.show()
    
    
    
             if __name__ == "__main__":
                 app = QtWidgets.QApplication(sys.argv)
                 app.setStyle(QtWidgets.QStyleFactory.create('Fusion'))  # won't work on windows style.
                 window = MyApp()
                 window.show()
                 sys.exit(app.exec_())
    



Aucun commentaire:

Enregistrer un commentaire