C++ (Qt)class ComboBoxProxyStyle : public QProxyStyle{ Q_OBJECT Q_PROPERTY(QString placeHolder READ placeHolder WRITE setPlaceHolder)public: ComboBoxProxyStyle(const QString& placeHolder) { setPlaceHolder(placeHolder); } virtual void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const { QStyleOption* opt = const_cast<QStyleOption*>(option); if (element == QStyle::CE_ComboBoxLabel) { QWidget* w = const_cast<QWidget*>(widget); QComboBox* cbx = qobject_cast<QComboBox*>(w); QStyleOptionComboBox* cb = qstyleoption_cast<QStyleOptionComboBox*>(opt); if (cb && cbx) { if (-1 == cbx->currentIndex()) { QPalette pal = cb->palette; pal.setBrush(QPalette::Text, pal.mid()); cb->currentText = placeHolder(); cb->palette = pal; } } } QProxyStyle::drawControl(element, opt, painter, widget); } virtual void drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole) const { if (placeHolder() == text) { textRole = QPalette::Text; } QProxyStyle::drawItemText(painter, rect, flags, pal, enabled, text, textRole); } QString placeHolder() const {return m_placeHolder;} void setPlaceHolder(const QString& text) {m_placeHolder = text;} private: QString m_placeHolder;};
C++ (Qt)ui->comboBox->setStyle(new ComboBoxProxyStyle("Select something..."));
C++ (Qt)cb->setEditable( true );cb->lineEdit()->setText( "Select something..." );cb->lineEdit()->setReadOnly( true );cb->lineEdit()->setEnabled( false );
C++ (Qt) QStandardItemModel* model = static_cast<QStandardItemModel*>(ui->comboBox->model()); QStandardItem* item = model->item(0, 0); Qt::ItemFlags flags = item->flags(); flags &= ~Qt::ItemIsSelectable & ~Qt::ItemIsEnabled; item->setFlags(flags);
C++ (Qt) QListView* listView = qobject_cast<QListView*>(ui->comboBox->view()); Q_CHECK_PTR(listView); listView->setRowHidden(0, true);