Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Behind the Scenes
  3. Qt.io webservices
  4. [SOLVED] Creating classes in Qt Stylesheet
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Creating classes in Qt Stylesheet

Scheduled Pinned Locked Moved Qt.io webservices
5 Posts 3 Posters 2.9k Views 2 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.
  • H Offline
    H Offline
    holygirl
    wrote on last edited by
    #1

    Hi all!

     I'm new to Qt and I have a question regarding stylesheets. I'm using many labels in my designed and I want each label to have a different styling. As of now, I have
    

    @ QLabel
    {
    border: none;
    padding: 0;
    background: none;
    }@

    but this applies the same style to all the labels. I even tried

    @.heading
    {
    color: brown;
    }

    .heading2
    {
    color: red;
    }@

    for different styles for each label. But I can't get it to work. My question is, how can I customize the styling for each label even though I am referring to the same stylesheet?
    Thanks.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      steno
      wrote on last edited by
      #2

      You can name your objects by calling QObject::setObjectName, then in the style sheet do the following

      c++
      @
      mLabel->setObjectName("Label1");
      @

      css
      @
      QLabel#Label1
      {
      color: brown;
      }
      @

      This will style all QLabels with the name of Label1.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        holygirl
        wrote on last edited by
        #3

        That worked! Thank you, steno.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Beliar
          wrote on last edited by
          #4

          Hello there.
          I'd like to necro this thread a little bit because I don't think using names is a very good option; afaik they are intended to be unique... at last that's the good practice imho.

          You could always subclass and use object type (i think), but that's too much work.

          Perhaps there's a way to attribute a proper c(/q)ss 'class' to objects?

          On a off-topic note: there's a typo in the member path, for example, this works:
          http://qt-project.org/mebmer/132333
          this doesn't:
          http://qt-project.org/member/132333
          It's funny :)

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Beliar
            wrote on last edited by
            #5

            Hello there.
            I'd like to necro this thread a little bit because I don't think using names is a very good option; afaik they are intended to be unique... at last that's the good practice imho.

            You could always subclass and use object type (i think), but that's too much work.

            Perhaps there's a way to attribute a proper c(/q)ss 'class' to objects?

            On a off-topic note: there's a typo in the member path, for example, this works:
            http://qt-project.org/mebmer/132333
            this doesn't:
            http://qt-project.org/member/132333
            It's funny :)

            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