Results 1 to 5 of 5

Thread: QComboBox::itemData() problem

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QComboBox::itemData() problem

    Hi to all!

    I've created combo box on some widget with:
    Qt Code:
    1. m_pDatabaseSelector=new QComboBox(this); // creates new combo box
    2. Q_CHECK_PTR(m_pDatabaseSelector); // checks creation
    To copy to clipboard, switch view to plain text mode 
    and then fill it with:
    Qt Code:
    1. m_pDatabaseSelector->addItem(strdbType); // adds db type to combo box
    2. m_pDatabaseSelector->addItem("ODBC"); // adds db type to combo box
    3. m_pDatabaseSelector->addItem("SQLite"); // adds db type to combo box
    To copy to clipboard, switch view to plain text mode 
    Combo box is shown and all item are in it as should be. Now, I've connected data selection signal with my slot:
    Qt Code:
    1. connect(m_pDatabaseSelector,
    2. SIGNAL(currentIndexChanged(int)),
    3. this,
    4. SLOT(itemChanged(int))); // signal connector
    To copy to clipboard, switch view to plain text mode 
    and here the code for itemChanged(int):
    Qt Code:
    1. void CDatabaseSettingsPage::itemChanged(int iSelectedIndex)
    2. {
    3. qDebug() << "iSelectedIndex:" << iSelectedIndex;
    4. qDebug() << "m_pDatabaseSelector->itemData(iSelectedIndex).toString():" << m_pDatabaseSelector->itemData(iSelectedIndex).toString();
    5. m_pAppSettings->updateRecord(databaseSettingKey,
    6. strDatabaseTypeKey,
    7. m_pDatabaseSelector->itemData(iSelectedIndex).toString());
    8. }
    To copy to clipboard, switch view to plain text mode 
    Well, itemData() returns empty string, iSelectedIndex is correclty setup. Why??
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QComboBox::itemData() problem

    QComboBox::itemData() returns the optional user data that can be associated to each item. I guess you're interested in QComboBox::itemText().
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QComboBox::itemData() problem

    Quote Originally Posted by jpn View Post
    QComboBox::itemData() returns the optional user data that can be associated to each item. I guess you're interested in QComboBox::itemText().
    Thanks, but just one more question: Why is then itemData() used in examples instead of itemText(), in examples there is also text retreived?
    Qt 5.3 Opensource & Creator 3.1.2

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QComboBox::itemData() problem

    itemData is internal, whereas itemText is user-visible.
    If you should ever need to translate your app, then you probably do not want your internals affected. Therefore it is often a good idea to be 'independent' of what (and how) some data is presented to the user.

    Moreover, you can store other (additional, different, more) stuff in itemData. E.g. you might show some text but need some internal numeric ID, which you may put in itemData.

    HTH

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QComboBox::itemData() problem

    Sorry, I had a dumb question, I deleted it, thanks for answers, problem has been solved.
    Last edited by MarkoSan; 18th June 2008 at 08:14. Reason: updated contents
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  2. problem with opengl, zooming, drawpixels, and origin
    By ntp in forum General Programming
    Replies: 0
    Last Post: 22nd February 2008, 21:48
  3. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  4. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.