Re: QComboBox roles question
I think the rules applied to QComboBox similar for all subclass of QAbstractItemView even QComboBox doesn't inherit QAbstractItemView, but It is by default, internally use QStandardItemModel and QComboBoxListView. Try to debug this after adding item and you'll get better understanding:
Code:
qDebug() << comboBox->model();
qDebug() << comboBox->view();
Re: QComboBox roles question
Honestly, I don't understand your suggestion. The following debug lines do not give me any useful info about data roles and why DisplayRole is used instead of EditRole and vice versa.
Re: QComboBox roles question
It is using Model/View convention.
Re: QComboBox roles question
Maybe I was not clear in asking my question. I am wondering why it shows EditRole data when I was expected to see DiplayRole data (in the drop down list, for example) or why does it show DisplayRole data in the edit field, moreover, I have different results depending on the order of data assignment to EditRole and DisplayRole. If you swap 16th and 17th line of my code you will get different results, that appears to be strange to me. Where model/view convention explains this?
Re: QComboBox roles question
Sometimes you really have to dig around in the docs to find answers ;)
From QComboBox
Quote:
By default a QStandardItemModel stores the items . . .
Then from QStandardItem::setData():
Quote:
Note: The default implementation treats Qt::EditRole and Qt:: DisplayRole as referring to the same data.
Re: QComboBox roles question
Quote:
Where model/view convention explains this?
noboro was right. You can look into the source, at line 808.
Re: QComboBox roles question
Thanks, guys. Now it's clear, but how can I change this behavior?
Re: QComboBox roles question
I suggest to subclass QStandardItem then set it as item prototype (QStandardItemModel::setItemPrototype).
Re: QComboBox roles question
You could try using a delegate. Something like this:
Code:
#include <QtGui>
class Delegate : public QStyledItemDelegate
{
return QString("item %1").
arg(value.
toString());
}
};
int main(int argc, char *argv[])
{
comboBox.setEditable(true);
for (int i = 0; i<5; ++i){
comboBox.
addItem(QString("%1").
arg(i
));
}
comboBox.setItemDelegate(new Delegate);
comboBox.show();
return a.exec();
}