[SOLVED] QComboBox with multiple columns
I'm trying to display multiple columns on a QComboBox. It's almost finished, with one exception: the visible item on the combo box only displays one column. When expanded, everything looks good. I wouldn't like to use a delegate. Sad thing is that once I had a code snippet that could do that...
Here is the relevant code for setting up the combo box:
QTableView *tblLan = new QTableView;
//m_source is a custom model derived from QAbstractListModel with 3 columns
//I only want to display the first 2 columns
//and the rest is code for making the tblLan appear like a list inside the combo box
Thanks in advance for any hints.
Up a little...
Is that the "code":http://www.qtcentre.org/threads/12768-Need-to-have-columns-in-QComboBox?p=67105#post67105 you were thinking about ?
[quote author="SGaist" date="1362473278"]Is that the "code":http://www.qtcentre.org/threads/12768-Need-to-have-columns-in-QComboBox?p=67105#post67105 you were thinking about ? [/quote]
Thanks for you reply. I've read this and probably all other related snippets on the net, but it won't work in my case. I'm trying to display a QIcon and a QString (the first and second column in my model) in the combo box.
If i understand correctly you want something like this?:
In that case, I would try to implement a proxy model that fetches the string for the text role and the icon for the decoration role. However I didn't test that so it's just an idea.
You already have custom model, just add decoration role case in your data method.
The data() method in my model is already implemented correctly. Actually, the very same model instance is used directly in a QTableView in the same QWizardPage where the QComboBox exists and it works fine. I think the problem lies on QComboBox's limitation to display only a single column (I suppose that's why there exists the setModelColumn() method, right?) for the current item although it allows multiple columns for the pop-up view.
Here are two pics that show the situation:
And the collapsed combo with some of the QWizardPage:
Here is some kind of pseudo code :)
@if(index.column() == columnWithText)
if (role == Qt::DecorationRole)
if (role == Qt::DisplayRole)
and for combo box setModelColumn(columnWithText)
someting like that. For more correct example you can put your data code here.
Thank you qxoz (and SGaist too, your idea was correct basically), your suggestion worked fine!
After I modified the data() method according to your suggestion, I just had to hide the first column from the views to avoid duplicating the QIcon. So basically, now the first column (QIcon) is bound to the second column (QString) for views, which is not a problem.
I'm glad to hear it :)