Results 1 to 3 of 3

Thread: QComboBox userData always returns invalid

  1. #1
    Join Date
    Sep 2013
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default QComboBox userData always returns invalid

    Hi,

    i try to use a ComboBox for a selection of Baudrates.
    I wan't to Display the Label for a Baudrate and store an unisigned int in the userData.

    Here's my Code:
    Qt Code:
    1. typedef struct
    2. {
    3. QString m_displayName;
    4. unsigned int m_Btr0Btr1;
    5. } baudrate_t;
    6.  
    7. ...
    8.  
    9. const baudrate_t available_bauds[]={ {"1 MBaud/s",0x014},
    10. {"800 kBaud/s",0x0016},
    11. {"500 kBaud/s",0x001C},
    12. {"250 kBaud/s",0x011C},
    13. {"125 kBaud/s",0x031C},
    14. {"100 kBaud/s",0x432F},
    15. {"95 kBaud/s",0xC34E},
    16. {"83 kBaud/s",0x852B},
    17. {"50 kBaud/s",0x472F},
    18. {"47 kBaud/s",0x1414},
    19. {"33 kBaud/s",0x8B2F},
    20. {"20 kBaud/s",0x532F},
    21. {"10 kBaud/s",0x672F},
    22. {"5 kBaud/s",0x7F7F},
    23. };
    24.  
    25. ...
    26.  
    27. for (int i = 0; i<( sizeof(available_bauds) / sizeof(available_bauds[0]) ); ++i)
    28. {
    29. QString Label = available_bauds[i].m_displayName;
    30. unsigned int BTR0BTR1 = available_bauds[i].m_Btr0Btr1;
    31. ui->cB_baudrate->addItem(Label,QVariant(BTR0BTR1));
    32. }
    33.  
    34. /*DEBUG Code*/
    35. qDebug() << ui->cB_baudrate->itemText(2);
    36. QVariant check_data = ui->cB_baudrate->itemData(2,Qt::UserRole);
    37. bool ok = false;
    38. unsigned int check_btr0btr1 = check_data.toUInt(&ok);
    39. if (ok)
    40. {
    41. qDebug() << check_btr0btr1;
    42. }
    43. /*END DEBUG CODE*/
    To copy to clipboard, switch view to plain text mode 

    the returned QVariant is always invalid.

    What am i doing wrong?

    OS Windows 7 64, QT 5.1.1, mingw48_32

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QComboBox userData always returns invalid

    What am i doing wrong?
    Apart from using confused units*... nothing I can see. It works fine here with Qt4.8.4 and Qt5.1.1 on Linux, and Qt 5.1.0 MingW on Windows.

    Of course, if that debug code runs before the combo box is loaded...


    * Either baud or bits per second, but not baud/sec. Baud and bits per second are not strictly the same thing either.

  3. #3
    Join Date
    Sep 2013
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: QComboBox userData always returns invalid

    confused units*
    You're right... shame on me

    I was running the for Loop for the Data fill in the Construktor of the Dialog (after setupUi()) and the Debug Code in a slot called by a Button click (the Displayed comboBox looks good and the qDebug for the text delivers me the right entry)

    Tried it with 5.1.0 and 5.0.2, same Problem, now I will try it in a complete new project, with nothing but the combobox

Similar Threads

  1. QComboBox UserData Role not saved?
    By tranfuga25s in forum Qt Programming
    Replies: 0
    Last Post: 29th December 2011, 17:04
  2. Replies: 6
    Last Post: 6th September 2010, 13:38
  3. "userdata" field in addItem in QComboBox
    By amulya in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2007, 12:15
  4. Retrieve userData of a comboBox item
    By jiveaxe in forum Newbie
    Replies: 6
    Last Post: 23rd August 2007, 10:28

Tags for this Thread

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.