Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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

    Hi
    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
    {
        Q_OBJECT
    public:
        ReadOnlyEdit(QWidget *parent = nullptr) : QLineEdit(parent) {}
    protected:
        virtual void keyPressEvent(QKeyEvent *event) override {} // eats keys
    };
    
    (in .cpp)
     ReadOnlyEdit *line = new ReadOnlyEdit(this);
     ui->comboBox->setLineEdit(line);
    

    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 https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qcombobox 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

    @JonB

    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