Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
920 views
in Technique[技术] by (71.8m points)

qt - Disable specific items in QComboBox

In my application, I want to disable some items (i.e. not selectable, no highlights when mouse hovering above, and the texts are greyed out) in the QComboBox when certain conditions are met.

I indeed found someone did ask the same question here: Disable Item in Qt Combobox But none of these solutions in the answers seem to actually work (including the trick).

Is there a decent and 'correct' way to implement this?

EDIT:

I found out why setting the flags wouldn't disable the items in my application: for some reasons, I had to set the style QStyle::SH_ComboBox_UseNativePopup(see https://codereview.qt-project.org/#/c/82718/). And this setting for some reasons blocked the flag setting. Does anyone have an idea why, and how to work around? A minimum test example is included (modified from the answer of @Mike):

#include <QApplication>
#include <QComboBox>
#include <QStandardItemModel>
#include <QProxyStyle>

class ComboBoxStyle : public QProxyStyle
{
public:
    int styleHint ( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = 0, QStyleHintReturn * returnData = 0 ) const override
    {
        if ( hint == QStyle::SH_ComboBox_UseNativePopup )
        {
            return 1;
        }
        return QProxyStyle::styleHint( hint, option, widget, returnData );
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QComboBox comboBox;

    // Setting this style would block the flag settings later on.
    comboBox.setStyle( new ComboBoxStyle() );

    comboBox.insertItem(0, QObject::tr("item1"));
    comboBox.insertItem(1, QObject::tr("item2"));

    QStandardItemModel* model = qobject_cast<QStandardItemModel*>(comboBox.model());
    QStandardItem* item= model->item(1);
    item->setFlags(item->flags() & ~Qt::ItemIsEnabled);

    comboBox.show();
    return a.exec();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The answer linked in my comment above seems to be talking about an old version of Qt. I have tested on Qt5.4 and Qt5.6 and there is no need set the color yourself here, you just need to set and/or clear the Qt::ItemIsEnabled flag, here is an example:

#include <QtWidgets>

int main(int argc, char *argv[]) {
  QApplication a(argc, argv);
  QComboBox comboBox;
  comboBox.addItem(QObject::tr("item1"));
  comboBox.addItem(QObject::tr("item2"));
  comboBox.addItem(QObject::tr("item3"));
  QStandardItemModel *model =
      qobject_cast<QStandardItemModel *>(comboBox.model());
  Q_ASSERT(model != nullptr);
  bool disabled = true;
  QStandardItem *item = model->item(2);
  item->setFlags(disabled ? item->flags() & ~Qt::ItemIsEnabled
                          : item->flags() | Qt::ItemIsEnabled);
  comboBox.show();
  return a.exec();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...