I'm using Pyside to create a combobox that draws from a sqlite database. Users can select one of the existing items or add a new item. Users see the item names (called "param"), but I need access to the item ids from the database. So, there are two steps:
READING ITEMS: I'm able to read from the database, but I can't display the item names while accessing the item ids behind the scenes.
ADDING ITEMS: Do I need to detect a change in the combobox and then use an SQL insert command or does the model handle this for me?
This code reads from the database but does not display correctly:
param_model = QSqlQueryModel()
param_model.setQuery("select id, param from partable order by param")
param_model.setHeaderData(0, Qt.Horizontal,"id")
param_model.setHeaderData(1, Qt.Horizontal,"param")
param_view = QTableView()
param_view.setColumnHidden(0,True)
self.paramfield = QComboBox()
self.paramfield.adjustSize()
self.paramfield.setEditable(True)
self.paramfield.setModel(param_model)
self.paramfield.setView(param_view)
There are several issues with your code. Firstly, you need to use a
QSqlTableModel
, which is editable, rather than aQSqlQueryModel
, which is read only. Secondly, you do not need to set headers or a view on the combo-box. Thirdly, you must set the correct model column on the combo-box in order to display the appropriate values.On the question of adding items: it is only necessary to submit the changes via the model. However, it is also often desirable to find the id or index of the item that was added (e.g. in order to reset the current index). This can be a little tricky if the model is sorted and/or allows duplicate entries.
The demo script below shows you how to deal with all the issues mentioned above:
PS: here's how to get the
id
from the combo-box, using its current index: