Details
-
Suggestion
-
Resolution: Done
-
P3: Somewhat important
-
None
-
None
-
360df2cf74410b05d1cab0192f2d0ff5a39db59e (qt/qtbase/5.15)
Description
A typical usecase for QComboBox is to display them initially with an "instruction" text, i.e. "Select country...". That text itself is not selectable, and disappears when the combobox is popped up the first time. The following subclass implements this behavior, but having this supported by QComboBox directly would be nice:
#include <QtGui> class ComboBox : public QComboBox { Q_OBJECT public: ComboBox() { } void setDummyItem(const QString &text) { dummyText = text; } void showPopup() { removeDummy(); QComboBox::showPopup(); } protected: void showEvent(QShowEvent *e) { addDummy(); QComboBox::showEvent(e); } private slots: void removeDummy() { if (!dummyText.isNull() && itemText(0) == dummyText) { removeItem(0); disconnect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(removeDummy())); } } private: void addDummy() { if (!dummyText.isNull() && itemText(0) != dummyText) { insertItem(0, dummyText); setCurrentItem(0); connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(removeDummy())); } } QString dummyText; }; #include "main.moc" int main(int argc, char *argv[]) { QApplication a(argc, argv); ComboBox cb; cb.addItems(QStringList() << "Euro" << "USD" << "Yen" << "GBP"); cb.setDummyItem("Select Currency..."); cb.show(); return a.exec(); }
Attachments
Issue Links
- is duplicated by
-
QTBUG-77141 Add a function to hide a QComboBox item
-
- Closed
-
-
QTBUG-2776 QComboBox: implement clearEditText for non-editable QComboBoxes
-
- Closed
-
For Gerrit Dashboard: QTBUG-1556 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
275041,10 | QComboBox: add property placeholderText | 5.15 | qt/qtbase | Status: MERGED | +2 | 0 |