Search code examples
cssqtselectorqwidget

How to select QWidget by class


Imagine, that I want to set a css style to some QWidgets in MainWindow, for example:

QPushButton {
    background: rgb(79, 79, 79);
}

But it applies css style to all QPushButtons, I would like to set background only to some QPushButtons identified by some class. How to do it?

Edit:

        <widget class="QPushButton" name="pushButton_2">
         <property name="sizePolicy">
          <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
           <horstretch>0</horstretch>
           <verstretch>0</verstretch>
          </sizepolicy>
         </property>
         <property name="minimumSize">
          <size>
           <width>0</width>
           <height>60</height>
          </size>
         </property>
         <property name="text">
          <string>Label</string>
         </property>
        </widget>

Solution

  • you need to use class selector

    https://www.w3schools.com/cssref/sel_class.asp

    QPushButton.someClass{
         background: rgb(79, 79, 79);
    }
    

    html will be e.g.

    <QPushButton class='someClass'>
        Label
    </QPushButton>