Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. White background for QComboBox despite not editable?

White background for QComboBox despite not editable?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 613 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    olowo726
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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.

      JonBJ 1 Reply Last reply
      0
      • mrjjM mrjj

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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?

        mrjjM 1 Reply Last reply
        2
        • JonBJ JonB

          @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?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @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.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved