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. CSS class selector not working with container widgets
QtWS25 Last Chance

CSS class selector not working with container widgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
cssstackedwidgetstylesheet
4 Posts 2 Posters 554 Views
  • 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.
  • A Offline
    A Offline
    ashbob999
    wrote on 5 May 2023, 21:13 last edited by
    #1

    Using QT5.15.2 msvc 32-bit.

    I have a window which contains the following structure:

    QMainWindow
    ---> QWidget
    -------> QTabWidget
    -----------> QComboBox
    

    Where the QWidget has a class property (class="className").
    Styling the QCombox only works correctly if it is created dynamically.

    Using the following css, the drop-down menu of the QComboBox only shows yellow if it is created dynamically.

    .className QComboBox QAbstractItemView {
        background-color: yellow;
    }
    

    Code to create QComboBox dynamically:

    QComboBox* box = new QComboBox();
    box->addItem("a");
    box->addItem("b");
    this->ui->tabWidget->currentWidget()->layout()->addWidget(bx2);
    

    But any QComboBox that is inside the QWidget but not in the QTabWidget is styled correctly. Or if I remove the .className selector from the css.

    So it seems like a problem with QTabWidget, and having .className and QAbstractItemView on the same css statement.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ashbob999
      wrote on 6 May 2023, 09:09 last edited by ashbob999 5 Jun 2023, 09:10
      #2

      After some testing it seems that because in the ui file the class properties are set after all the widgets are created and added, the QComboBoxListView private class's style does not get updated.

      But if the class properties are added straight after the widget is created, the it is styled correctly.

      Ui file from:

      void setupUi(...) {
          widget = new QWidget(...);
          comboBox = new QComboBox(widget);
      
          ...
      
          retranslateUi(MainWindow); // sets class properties
      }
      

      To

      void setupUi(...) {
          widget = new QWidget(...);
          widget.setProperty("class", "test"); // coptied from restranslateUi function
          comboBox = new QComboBox(widget);
      
          ...
      
          retranslateUi(MainWindow); // sets class properties
      }
      

      Obviously this is not a usable solution because the ui_*.h file is auto generated.

      Another way to fix this is to re-polish the QComboBoxListView by recursively re-polishing QComboBox and it's children

      void recursively_polish(QObject* object) {
          auto* widget = dynamic_cast<QWidget*>(object);
          if (widget!=nullptr){
              widget->style()->unpolish(widget);
              widget->style()->polish(widget);
          }
      
          auto children = object->findChildren<QObject*>(QString(), Qt::FindDirectChildrenOnly);
          for (auto& child : children){
              polish_recursive(child);
          }
      }
      

      Is this the only way to fix it, or is there another better way?

      J 1 Reply Last reply 6 May 2023, 09:16
      1
      • A ashbob999
        6 May 2023, 09:09

        After some testing it seems that because in the ui file the class properties are set after all the widgets are created and added, the QComboBoxListView private class's style does not get updated.

        But if the class properties are added straight after the widget is created, the it is styled correctly.

        Ui file from:

        void setupUi(...) {
            widget = new QWidget(...);
            comboBox = new QComboBox(widget);
        
            ...
        
            retranslateUi(MainWindow); // sets class properties
        }
        

        To

        void setupUi(...) {
            widget = new QWidget(...);
            widget.setProperty("class", "test"); // coptied from restranslateUi function
            comboBox = new QComboBox(widget);
        
            ...
        
            retranslateUi(MainWindow); // sets class properties
        }
        

        Obviously this is not a usable solution because the ui_*.h file is auto generated.

        Another way to fix this is to re-polish the QComboBoxListView by recursively re-polishing QComboBox and it's children

        void recursively_polish(QObject* object) {
            auto* widget = dynamic_cast<QWidget*>(object);
            if (widget!=nullptr){
                widget->style()->unpolish(widget);
                widget->style()->polish(widget);
            }
        
            auto children = object->findChildren<QObject*>(QString(), Qt::FindDirectChildrenOnly);
            for (auto& child : children){
                polish_recursive(child);
            }
        }
        

        Is this the only way to fix it, or is there another better way?

        J Offline
        J Offline
        JonB
        wrote on 6 May 2023, 09:16 last edited by
        #3

        @ashbob999
        I had been going to ask what you had done about polishing, given your behaviour, but you have answered. So far as I know this is indeed required/best/simplest, see discussion Is there a better method than calling polish recursively on all children of a QWidget?

        A 1 Reply Last reply 6 May 2023, 13:56
        0
        • J JonB
          6 May 2023, 09:16

          @ashbob999
          I had been going to ask what you had done about polishing, given your behaviour, but you have answered. So far as I know this is indeed required/best/simplest, see discussion Is there a better method than calling polish recursively on all children of a QWidget?

          A Offline
          A Offline
          ashbob999
          wrote on 6 May 2023, 13:56 last edited by
          #4

          @JonB Thats fine, at least I don't have to unpolish first.
          Although I still think there is a bug because you don't have to polish the QComboBox's ItemView when it is not inside a QTabWidget or QStackedWidget.

          1 Reply Last reply
          0

          2/4

          6 May 2023, 09:09

          • Login

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