I want a drop-down category selection box which allows the user to click multiple selections, and which preferably is connected to an SQL query. I'm not attached to the Combobox in particular (disappearing on each click isn't helpful), whatever can get the job done.
At the current moment, I have basically that jerry-rigged together, it allows for multiple selections technically but it's just based on randomly if the mouse is dragged over it or not.
self.catbx=QComboBox()
...
self.catq=QtSql.QSqlQuery(conn)
self.catq.exec("SELECT name FROM categories")
self.catmo=QtSql.QSqlQueryModel()
self.catmo.setQuery(self.catq)
self.catbx.setModel(self.catmo)
...
self.catview=QListView()
self.catview.setModel(self.catmo)
self.catbx.setView(self.catview)
self.catview.setSelectionMode(QAbstractItemView.MultiSelection)
hope that's clear enough and someone can help! :)
Basically it is that the items are not selectable, since that is the event that triggers the closing of the popup so the solution is to eliminate that flag in the model as I did in a previous answer.
On the other hand the option to be checkeable does not come by default in the
QSqlQueryModel
, so we have to implement it for that we based on another previous answer.Finally, a class is created that inherits from
QComboBox
and we overwrite thehidePopup()
method to emit a signal that sends the selected items.Update:
If you want to also be marked when you press any part of the item you must create a delegate and overwrite the
editorEvent()
method so that it handles theMouseButtonRelease
event. But this brings a small problem: to open the popup you have to press the item that is displayed so it will open marked.