Anyone using the Qt 3.x libraries have probably run into the occasional oddity, however what I’ve discovered is a little too odd to not mention. When dealing with combo boxes, you generally find helper functions that provide accessors / modifiers to the currently selected item in the combo box. In the 3.x Qt libraries, one would assume that would be the setCurrentText function. Especially after this documentation:
void QComboBox::setCurrentText ( const QString& ) // [virtual] //Sets the text of the combobox's current item. See the "currentText" property for details. QString currentText() //This property holds the text of the combobox's current item. //Set this property's value with setCurrentText() and get this property's value with currentText().
Unfortunately, after further investigation into the source code, one can quickly find that it’s not that simple. Here’s how it works:
IF you give QComboBox::setCurrentText(QString&) a string that does not match an already existing entry, then the currenty entry is changed to that given string. However, if the given string DOES match and existing entry, then that matching entry is selected, and nothing is change and/or set (not quite what the documentation indicates).
Example:
QComboBox myCBX; myCBX.insertItem("Item1", 1); myCBX.insertItem("Item2", 2); myCBX.insertItem("Item3", 3); myCBX.setCurrentItem(1); // "Item1" is now selected myCBX.setCurrentText("Item5"); // "Item1" at index 1 changes to "Item5" myCBX.setCurrentText("Item3"); // currentItem changed to index 3;
The simple replacement to this is QComboBox::changeItem(“Matching String”, QComboBox::currentItem()).
myCBX.changeItem("Item2", myCBX.currentItem()); // "Item3" at index 3 changes to "Item2" even // though it's value is the same as "Item2" at index 2
Fortunately, it appears that they have removed this misleading function call in Qt 4.