PDA

View Full Version : QComboBox::itemData() problem



MarkoSan
17th June 2008, 23:23
Hi to all!

I've created combo box on some widget with:
m_pDatabaseSelector=new QComboBox(this); // creates new combo box
Q_CHECK_PTR(m_pDatabaseSelector); // checks creationand then fill it with:
m_pDatabaseSelector->addItem(strdbType); // adds db type to combo box
m_pDatabaseSelector->addItem("ODBC"); // adds db type to combo box
m_pDatabaseSelector->addItem("SQLite"); // adds db type to combo boxCombo box is shown and all item are in it as should be. Now, I've connected data selection signal with my slot:
connect(m_pDatabaseSelector,
SIGNAL(currentIndexChanged(int)),
this,
SLOT(itemChanged(int))); // signal connectorand here the code for itemChanged(int):
void CDatabaseSettingsPage::itemChanged(int iSelectedIndex)
{
qDebug() << "iSelectedIndex:" << iSelectedIndex;
qDebug() << "m_pDatabaseSelector->itemData(iSelectedIndex).toString():" << m_pDatabaseSelector->itemData(iSelectedIndex).toString();
m_pAppSettings->updateRecord(databaseSettingKey,
strDatabaseTypeKey,
m_pDatabaseSelector->itemData(iSelectedIndex).toString());
}Well, itemData() returns empty string, iSelectedIndex is correclty setup. Why??

jpn
17th June 2008, 23:38
QComboBox::itemData() returns the optional user data that can be associated to each item. I guess you're interested in QComboBox::itemText().

MarkoSan
18th June 2008, 00:04
QComboBox::itemData() (http://doc.trolltech.com/latest/qcombobox.html#itemData) returns the optional user data that can be associated to each item. I guess you're interested in QComboBox::itemText() (http://doc.trolltech.com/latest/qcombobox.html#itemText).

Thanks, but just one more question: Why is then itemData() used in examples instead of itemText(), in examples there is also text retreived?

caduel
18th June 2008, 08:17
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

MarkoSan
18th June 2008, 09:13
Sorry, I had a dumb question, I deleted it, thanks for answers, problem has been solved.:D