Is it possible to replace visible QComboBox values with an custom text?
-
Hey everyone,
I have a QComboBox that is populated with data via a QDataWidgetMapper with a model. This model contains an ID and an image file from the database.
Let's assume that the data set stored in the QComboBox has three values. For illustration I take the ID field and not the Blob field. The QComboBox will now display the following:
7036
7036
7036This shows me now that I can select 3 images from a QComboBox. Of course it would be nicer if the 7036 becomes an image 1, 2, 3, so for example:
7036 -> Image 1
7036 -> Image 2
7036 -> Image 3Is such a thing possible and if so how? Unfortunately I could not find anything on the Internet.Below is another screenshot. I would like to replace these numbers with Image1, Image 2, Image 3.
-
Thank you very much for your answers. After many attempts to get the whole thing to work, I have then decided for a somewhat easier way. I have simply created a new field with "filename" in my database table. I now display this in the QComboBox and use it to select the data.
-
Hi,
I have not tried it, I am by no means an expert and haven't used QWidgets for years, but if you want to do complicated things I'd try to inherit from QComboBox and overwriteQString QComboBoxPrivate::itemText(const QModelIndex &index) const { return index.isValid() ? model->data(index, itemRole()).toString() : QString(); }
with something that gets the proper text displayed.
But wouldn't it suffice to state the visible column?
The docs seem to suggest that:modelColumn : int This property holds the column in the model that is visible. If set prior to populating the combo box, the pop-up view will not be affected and will show the first column (using this property's default value). By default, this property has a value of 0. Note: In an editable combobox, the visible column will also become the completion column. Access functions: int modelColumn() const void setModelColumn(int visibleColumn)
But, as stated before, all of this is just a guess ymmv.
-
Hi,
QComboBox uses Qt's item view system so you can use a custom QStyledItemDelegate to show what you want.
-
Thank you very much for your answers. After many attempts to get the whole thing to work, I have then decided for a somewhat easier way. I have simply created a new field with "filename" in my database table. I now display this in the QComboBox and use it to select the data.