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. [SOLVED] Creating classes in Qt Stylesheet
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Creating classes in Qt Stylesheet

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 7.0k 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.
  • H Offline
    H Offline
    holygirl
    wrote on 18 Jan 2013, 08:49 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
    1
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 18 Jan 2013, 08:57 last edited by
      #2

      You need to use id selector:
      @
      QLabel#myObjectName {}
      @

      myObjectName is the objectName property of QObject (QObject::setObjectName()). More about QSS here: "link":http://qt-project.org/doc/qt-4.8/stylesheet-syntax.html.

      Just be sure not to overload the thing. QSS parsing takes time and can noticeably impact the performance if it grows too big.

      (Z(:^

      1 Reply Last reply
      1
      • H Offline
        H Offline
        holygirl
        wrote on 18 Jan 2013, 09:44 last edited by
        #3

        Thanks for the reply sierdzio. I went through the document and tried implementing the id selector but I think I'm doing something wrong. In my .qss, I have

        @
        QLabel#heading
        {
        color: brown;
        }
        @

        and in my code, I call it as

        @
        ui->label->setObjectName("heading");
        @

        Is this syntax right? Could you please guide me in the right direction?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 18 Jan 2013, 09:47 last edited by
          #4

          It is correct, yes.

          (Z(:^

          1 Reply Last reply
          0
          • H Offline
            H Offline
            holygirl
            wrote on 18 Jan 2013, 10:00 last edited by
            #5

            That's odd. My label doesn't seem to be brown in color! I've also included the stylesheet in my main.cpp as

            @
            QFile styleFile( "://new//prefix1//the-path.qss" );
            styleFile.open( QFile::ReadOnly );
            QString style( styleFile.readAll() );
            app.setStyleSheet( style );
            @

            Is there anything else I need to include? Could you please a provide an example? You've been a lot of help to me. Thanks.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 18 Jan 2013, 10:16 last edited by
              #6

              Wow, and it works in general? Qt uses unix paths. No need to use double forward slashes like that.

              I don't know the answer. This code should be enough. Most probably the object name is changed after stylesheet is loaded, so it is not updated. Try either loading QSS later, or run ui->label->setStyleSheet(""); after you set the object name. Maybe that would trigger a redraw.

              (Z(:^

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jazzycamel
                wrote on 18 Jan 2013, 10:32 last edited by
                #7

                The following is a full working example with C++, QSS and QRC sources:

                main.cpp
                @
                #include <QtGui>

                class Widget : public QWidget
                {

                public:
                Widget(QWidget *parent = 0){
                Q_UNUSED(parent)

                    QVBoxLayout *l=new QVBoxLayout(this);
                    QLabel *hLabel=new QLabel("Heading Label", this);
                    hLabel->setObjectName("headingLabel");
                    l->addWidget(hLabel);
                
                    l->addWidget(new QLabel("Other Label", this));
                }
                

                };

                int main(int argc, char *argv[])
                {
                QApplication a(argc, argv);

                QFile sFile&#40;":/main.qss"&#41;;
                if(sFile.open(QFile::ReadOnly)){
                    QString style(sFile.readAll());
                    a.setStyleSheet(style);
                }
                
                Widget w;
                w.show();
                return a.exec&#40;&#41;;
                

                }
                @

                main.qss
                @
                QLabel {
                background-color: pink;
                }

                QLabel#headingLabel {
                color: brown;
                }
                @

                example.qrc
                @
                <RCC>
                <qresource>
                <file>main.qss</file>
                </qresource>
                </RCC>
                @

                Hope this helps ;o)

                For the avoidance of doubt:

                1. All my code samples (C++ or Python) are tested before posting
                2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  holygirl
                  wrote on 18 Jan 2013, 10:52 last edited by
                  #8

                  sierdzio

                  You were right! Running

                  @
                  ui->label->setStyleSheet(”“);
                  @

                  worked like a charm! Thank you so much for your time :)

                  @jazzycamel: Thanks to you too for providing a nice example.

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    ochampao
                    wrote on 7 Aug 2013, 16:26 last edited by
                    #9

                    Hello,

                    Is there a way to have the same result as @jazzycamel example on the hLabel but without registering the object using the setObjectName() method?

                    In other words, specify within the stylesheet directly that the style must be applied to hLabel only which is a member of the class Widget.

                    Why is the setObjectName() necessary?

                    The reason I am asking this is, let's say you have a series of custom dialogs (e.g. dialog1-dialog3) with each one having as a member a QPushButton with the same name "nextButton". Then using the setObjectName() approach one needs to register each "nextButton" with a different identifier if he wants to apply a separate style to each one.

                    What I am looking for is a way to identify directly in the stylesheet each nextButton from the dialog it is a member of.

                    Thanks for your help.

                    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