PDA

View Full Version : [Qt 4] QComboBox :: findData



doc
12th July 2006, 19:45
I have a problem with QCombo's findData function which is always returning -1 value.

---SerialPortPage.cpp---



//I have declared metatype of custom StopBitsType.
Q_DECLARE_METATYPE(StopBitsType)

...

//then add some items to QComboBox
stopCombo->addItem("1", qVariantFromValue(STOP_1));
#ifdef _TTY_WIN_
stopCombo->addItem("1.5", qVariantFromValue(STOP_1_5));
#endif /*_TTY_WIN_*/
stopCombo->addItem("2", qVariantFromValue(STOP_2));

...

//this is working very well, returning value of proper StopBitsType
stopCombo->itemData(stopCombo->currentIndex()).value<StopBitsType>()

...

//however findData is always returning -1...
stopCombo->findData(qVariantFromValue(STOP_2)) //printf or something



Am I doing something wrong :/ ? Have you ever use this function ?
I didn't find any examples or information except short description in the documentation.



int QComboBox::findData ( const QVariant & data, int role = Qt::UserRole, Qt::MatchFlags flags = Qt::MatchExactly | Qt::MatchCaseSensitive ) const
Returns the index of the item containing the given data for the given role; otherwise returns -1.
The flags specify how the items in the combobox are searched.


The StopBitsType is defined as:


---qextserialbase.h---



typedef enum _StopBitsType {
STOP_1,
STOP_1_5, //WINDOWS ONLY
STOP_2
} StopBitsType;

jacek
12th July 2006, 20:10
findData() works OK for me.

The problem might be in Q_DECLARE_METATYPE --- IMO you can't use it for enums.

qRegisterMetaType() docs say:
Any class or struct that has a public constructor, a public copy constructor, and a public destructor can be registered.and Q_DECLARE_METATYPE uses qRegisterMetaType() internally.