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. Qt Widgets global stylesheet

Qt Widgets global stylesheet

Scheduled Pinned Locked Moved Solved General and Desktop
qtwidgetsstylesheet
8 Posts 4 Posters 13.4k 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.
  • T Offline
    T Offline
    Tikani
    wrote on last edited by
    #1

    Does Qt allow to set stylesheets application-wide? It's very tedious to set them to each control by hand. It would be perfect if Qt allows to work with stylesheets like with an ordinary CSS - to write global selectors, make strict targeting with "id" attribute and so on.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Yes !
      And its also recommended to do so
      as setting on each widget becomes a mess.
      QApplication have method.
      http://doc.qt.io/qt-5/qapplication.html#styleSheet-prop

      and yes, it has selectors as ClassType, name etc

      T 1 Reply Last reply
      4
      • mrjjM mrjj

        Yes !
        And its also recommended to do so
        as setting on each widget becomes a mess.
        QApplication have method.
        http://doc.qt.io/qt-5/qapplication.html#styleSheet-prop

        and yes, it has selectors as ClassType, name etc

        T Offline
        T Offline
        Tikani
        wrote on last edited by
        #3

        @mrjj
        Ok, I understood that.
        Then I want to put a huge string literal that contains global stylesheet to project resources and pass it to the method you pointed above. Does Qt allow to move "hardcoded" strings to resources?

        mrjjM M 2 Replies Last reply
        0
        • T Tikani

          @mrjj
          Ok, I understood that.
          Then I want to put a huge string literal that contains global stylesheet to project resources and pass it to the method you pointed above. Does Qt allow to move "hardcoded" strings to resources?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Tikani
          Yes, you can easy load a text file/stylesheet from resources. just use the special ":/" syntax and it just works
          Note that you must use QFile to load it.

          1 Reply Last reply
          2
          • T Tikani

            @mrjj
            Ok, I understood that.
            Then I want to put a huge string literal that contains global stylesheet to project resources and pass it to the method you pointed above. Does Qt allow to move "hardcoded" strings to resources?

            M Offline
            M Offline
            mostefa
            wrote on last edited by mostefa
            #5

            @Tikani said in Qt Widgets global stylesheet:

            @mrjj
            Ok, I understood that.
            Then I want to put a huge string literal that contains global stylesheet to project resources and pass it to the method you pointed above. Does Qt allow to move "hardcoded" strings to resources?

            I think that the best practise for qt stylesheet is to use a ressource file on the qt ressource file named stylesheet.qss for example,

            You stylesheet.qss will contain this for example:

            stylesheet.qss

            QWidget
            {
            background-color: red;
            }
            QPushButton
            {
            color: yellow;
            }
            and then on your application you have to do something like this:

            QFile File("stylesheet.qss");
            File.open(QFile::ReadOnly);
            QString StyleSheet = QLatin1String(File.readAll());
            
            qApp->setStyleSheet(StyleSheet);
            

            I hope this can help you !

            1 Reply Last reply
            2
            • T Offline
              T Offline
              Tikani
              wrote on last edited by
              #6

              @mrjj, @mostefa
              Big thanks to both of you, I got the point.
              Marking as solved.

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                Hi
                And for fooling around and testing you can do also do

                auto teststyle = R"(
                               QWidget
                               {
                               background-color: red;
                               }
                               QPushButton
                               {
                               color: yellow;
                               }
                               )";
                
                int main(int argc, char* argv[]) {
                  QApplication a(argc, argv);
                  a.setStyleSheet(teststyle);
                  MainWindow w;
                  w.show();
                  return a.exec();
                }
                (c++ 11 compiler)
                
                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  GeminiMan
                  wrote on last edited by
                  #8

                  If you want stylesheet ready for the use of qss in your application use this site:
                  https://qss-stock.devsecstudio.com

                  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