I am trying to understand how data roles are used by QComboBox class. I want to add items that are displayed in one way, but edited in another way, e.g. item is displayed as "Item 1", but when I select it, it should be placed to the edit field as "1". I tried to use Qt:isplayRole and Qt::EditRole, but I noticed something strange: it seems like values are displayed for the role I used for the last time. In following example items are shown for the EditRole, even if I set combobox as not editable, but it worth to swap those two lines and DisplayRole will be used everywhere, even in the edit field. Could somebody explain me thing behavior?

Qt Code:
  1. #include <QtCore>
  2. #include <QApplication>
  3. #include <QtGui>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication a(argc, argv);
  8.  
  9. QComboBox comboBox;
  10. comboBox.setEditable(true);
  11.  
  12. for (int i = 0; i<3; ++i)
  13. {
  14. comboBox.addItem(QString("item %1").arg(i));
  15.  
  16. comboBox.setItemData(i, QString("display %1").arg(i), Qt::DisplayRole);
  17. comboBox.setItemData(i, i, Qt::EditRole);
  18. }
  19.  
  20. comboBox.show();
  21.  
  22. return a.exec();
  23. }
To copy to clipboard, switch view to plain text mode