Results 1 to 10 of 10

Thread: QComboBox roles question

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default QComboBox roles question

    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 

  2. #2
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default 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:
    Qt Code:
    1. qDebug() << comboBox->model();
    2. qDebug() << comboBox->view();
    To copy to clipboard, switch view to plain text mode 
    ~ We are nothing in this universe ~

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default 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.

  4. #4
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QComboBox roles question

    It is using Model/View convention.
    ~ We are nothing in this universe ~

  5. #5
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default 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?

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QComboBox roles question

    Sometimes you really have to dig around in the docs to find answers

    From QComboBox
    By default a QStandardItemModel stores the items . . .
    Then from QStandardItem::setData():
    Note: The default implementation treats Qt::EditRole and Qt:: DisplayRole as referring to the same data.

  7. The following user says thank you to norobro for this useful post:

    mentalmushroom (27th April 2012)

  8. #7
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QComboBox roles question

    Where model/view convention explains this?
    noboro was right. You can look into the source, at line 808.
    ~ We are nothing in this universe ~

  9. The following user says thank you to viulskiez for this useful post:

    mentalmushroom (27th April 2012)

  10. #8
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QComboBox roles question

    Thanks, guys. Now it's clear, but how can I change this behavior?

  11. #9
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QComboBox roles question

    I suggest to subclass QStandardItem then set it as item prototype (QStandardItemModel::setItemPrototype).
    ~ We are nothing in this universe ~

  12. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QComboBox roles question

    You could try using a delegate. Something like this:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Delegate : public QStyledItemDelegate
    4. {
    5. QString displayText(const QVariant &value, const QLocale &locale) const{
    6. return QString("item %1").arg(value.toString());
    7. }
    8. };
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication a(argc, argv);
    13. QComboBox comboBox;
    14. comboBox.setEditable(true);
    15. for (int i = 0; i<5; ++i){
    16. comboBox.addItem(QString("%1").arg(i));
    17. }
    18. comboBox.setItemDelegate(new Delegate);
    19. comboBox.show();
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Qcombobox and model/item based question
    By yaguis in forum Newbie
    Replies: 5
    Last Post: 7th June 2010, 01:22
  2. Getting all roles of specified column and/or model
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 29th April 2010, 11:21
  3. QSqlQueryModel + user defined roles
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2009, 07:29
  4. Basic question on QComboBox
    By Raccoon29 in forum Newbie
    Replies: 2
    Last Post: 9th May 2008, 16:41
  5. Language Selector QCombobox question
    By MarkoSan in forum Qt Programming
    Replies: 5
    Last Post: 31st January 2008, 08:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.