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. Using CSS class
Forum Update on Tuesday, May 27th 2025

Using CSS class

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 10.4k 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.
  • T Offline
    T Offline
    ThaRez
    wrote on 1 Jun 2012, 13:28 last edited by
    #1

    Hello
    Ho can I create to groups of QLabels, styled with CSS? So that all the QLabels belonging to one of the groups have similar properties, and
    the ones belonging to the other has similar as well. using #label_name I can style a single QLabel, but what should I refer to to apply the style to a whole class .labelclass{...} ? thanks
    -Richard

    1 Reply Last reply
    0
    • F Offline
      F Offline
      francomartins
      wrote on 1 Jun 2012, 13:44 last edited by
      #2

      I'm using as well to set background of all QLabel of the my Project :

      @
      this->setStyleSheet("QLabel "
      "{"
      "background-color: rgb(255, 255, 255);"
      "};");
      @
      ...

      it's Woking 100%

      1 Reply Last reply
      0
      • T Offline
        T Offline
        ThaRez
        wrote on 4 Jun 2012, 09:52 last edited by
        #3

        My problem is that I have have different "groups" of labels that have different appearances. What I'm trying to achieve is something similar to

        @QLabel#firstlabel { background-color: red;}
        QLabel#secondlabel { background-color: green;} @

        This works fine for individual items were I can refer using the label name, but how can I solve the situation with 20 "firstlabels" and 20 "secondlabels" without defining the styles for each and everyone separately?

        • Richard
        1 Reply Last reply
        0
        • M Offline
          M Offline
          Maksim Sidorov
          wrote on 20 Dec 2012, 12:22 last edited by
          #4

          I hope you already find the solution, but because this topic is in first places in google, when you search "qt css class", I will try to give a proper answer. Don't call me a necroposter, please =)

          At first need to add "class" property to your QObject. Then set this property with some definition, in this example it is "my_supa_button".

          @
          QVariant result = MyLabel.property( "class" );
          if( result.isValid() )
          {
          MyLabel.setProperty( "class", "my_supa_button" );
          }
          @

          Then in .css file need to add css class "my_supa_button" implementation:

          @
          *[class=my_supa_button] {
          font: 50px;
          color: black;
          }
          @

          So in this example every QObject can use this stylesheet, if it have QProperty "class" with value "my_supa_button".

          I hope my answer will help someone =)

          1 Reply Last reply
          1
          • G Offline
            G Offline
            gehrmann
            wrote on 18 Oct 2013, 20:15 last edited by
            #5

            [quote author="Kragius" date="1356006166"]
            I hope my answer will help someone =)
            [/quote]

            Great Thanks, Kragius!

            1 Reply Last reply
            0
            • V Offline
              V Offline
              VRonin
              wrote on 18 Oct 2013, 23:07 last edited by
              #6

              If you don't need to use QObject::findChild() then you can assign the same name (through setObjectName() ) to multiple items. names are not required to be unique. Then you can specify the stylesheet as you specify in your second post

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0

              • Login

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