Important: Please read the Qt Code of Conduct -

White background for QComboBox despite not editable?

  • I have a QComboBox from which the user shall be able to make his/her pick from the available options but not write arbitrary data. To make this happen I set the QComboBox to not editable. But then the background turns grey, i.e. it appears disabled. Is this really intentional or am I doing something wrong? How can I make it appear enabled?

  • Lifetime Qt Champion

    Nope, it draws like that.
    When truly disabled, the text is grey also.
    But I agree. It does look disabled.

    You could do

    class ReadOnlyEdit : public QLineEdit
        ReadOnlyEdit(QWidget *parent = nullptr) : QLineEdit(parent) {}
        virtual void keyPressEvent(QKeyEvent *event) override {} // eats keys
    (in .cpp)
     ReadOnlyEdit *line = new ReadOnlyEdit(this);

    It does show a cursor, so might not be what you are looking for, though.
    But it will show as normal white and allow no editing.

  • @mrjj , @olowo726
    I haven't investigated any further, but can't you also do this via stylesheet? In the example at you can see they are using QComboBox:!editable selector, can that be used to set the background wanted, without having to write code?

  • Lifetime Qt Champion


    Yes you can ! I just forgot to write it 🙄 :)
    The only thing is - that sometimes adding a stylesheet, alters widgets looks.
    But as far as I recall, it's ok with QComboBox:!editable without Combobox start looking odd.

Log in to reply