From Qt Assistant:
void QComboBox::setView ( QAbstractItemView * itemView ):
Sets the view to be used in the combobox popup to the given itemView. The combobox takes ownership of the view.
Note: If you want to use the convenience views (like QListWidget, QTableWidget or QTreeWidget),
make sure to call setModel() on the combobox with the convenience widgets model before calling this function.
Example: (written in Python)
box.setObjectName('somename')
model.setQuery('select something as somename, somethingother from somewhere')
view.setModel(model)
#Do with the view whatever you like
view.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
view.setAutoScroll(False)
view.resizeColumnsToContents()
view.resizeRowsToContents()
#view.setSortingEnabled(True)
view.verticalHeader().setVisible(False) #Rownumbers
view.setMinimumWidth(view.horizontalHeader().length())
#The important part: First set the model, then the view on the box
box.setModel(model)
box.setView(view)
box = QtGui.QComboBox()
box.setObjectName('somename')
view = QtGui.QTableView()
model = QtSql.QSqlQueryModel()
model.setQuery('select something as somename, somethingother from somewhere')
view.setModel(model)
#Do with the view whatever you like
view.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
view.setSelectionMode(QAbstractItemView.SingleSelection)
view.setSelectionBehavior(QAbstractItemView.SelectRows)
view.setAutoScroll(False)
view.resizeColumnsToContents()
view.resizeRowsToContents()
#view.setSortingEnabled(True)
view.verticalHeader().setVisible(False) #Rownumbers
view.setMinimumWidth(view.horizontalHeader().length())
#The important part: First set the model, then the view on the box
box.setModel(model)
box.setView(view)
To copy to clipboard, switch view to plain text mode
As with every solution, it comes with new problems. In the previous example, it is possible to set the QComboBox with any value from the QTable. Or, in other words, if you click the first value from the first row the QComboBox will show a different value as when you would click the second value from the first row.
I'm open to suggestions as how to solve that problem.
Bookmarks