PyQt6 Selecting multiple items from a QComboBox
-
I have a QComboBox populated from a database table. I want the user to be able to select one or more items to search other tables in the database. I considered the following to be logical but it's not permissible:
self.tagComboBox.setSelectionMode(QComboBox.InsertPolicy.QAbstractItemView.MultiSelection)My code to establish the QComboBox and window:
from PyQt6.QtCore import Qt from PyQt6.QtGui import QPixmap from PyQt6.QtWidgets import ( QApplication, QCheckBox, QComboBox, QDial, QDoubleSpinBox, QLabel, QLineEdit, QListWidget, QMainWindow, QSlider, QAbstractItemView, QSpinBox ) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("SQL Lite tags to combobox") self.tagComboBox = QComboBox() self.tagComboBox.setInsertPolicy(QComboBox.InsertPolicy.InsertAlphabetically) # self.tagComboBox.setSelectionMode(QComboBox.InsertPolicy.QAbstractItemView.MultiSelection) self.tagComboBox.setEditable(True) self.tagComboBox.currentIndexChanged.connect(self.index_changed) # Sends a signal with the new index value self.tagComboBox.currentTextChanged.connect(self.text_changed) # Sends a signal with the new text self.setCentralWidget(self.tagComboBox)
-
Quick google search:
Or you translate this code to Python:
Anyway, why do you want to use a
QComboBox
as it's not intended to be used this way?!
Maybe aQListWidget
/QListView
is more fitting...
(there you have proper selection model support and it's more intuitive than a dropdown, where you have to click multiple times and close it manually again) -
@Pl45m4 Thank you. My Google-foo (or perhaps it's Bing-fooey!), lacks your power!
And, as to your second question: Because I'm still finding my way around Qt. I didn't really like the ComboBox behaviour, I'll have a look at the alternatives you suggested.