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. Changing QPalette::WindowText by Q_PROPERTY by QSS
Forum Updated to NodeBB v4.3 + New Features

Changing QPalette::WindowText by Q_PROPERTY by QSS

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 2.4k 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.
  • R Offline
    R Offline
    RyuMake
    wrote on last edited by RyuMake
    #1

    Hi,

    I'm trying to use a Q_PROPERTY to set a value to QPalette, for example WindowText, ButtonText, via an external stylesheet.
    I have no issue using them elsewhere, on QPainter etc, but when I try to use them here I only get a black color.

    I'm very new to Qt, and a little rusty at C++, any help is greatly appreciated.

    main.cpp

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        qApp->setStyle(QStyleFactory::create("Fusion"));
    
        QFile File(":/styles/stylesheet.qss");
        File.open(QFile::ReadOnly);
        QString StyleSheet = QLatin1String(File.readAll());
        qApp->setStyleSheet(StyleSheet);
    
        Theme theme;
    
        QPalette p;
        p.setColor(QPalette::WindowText, theme->getClr);
        qApp->setPalette(p);
    }
    

    class.h

    class Theme : public QWidget
    {
        Q_OBJECT
        Q_PROPERTY(QColor clr READ getClr WRITE setClr DESIGNABLE true)
    
    public:
        explicit Theme(QWidget *parent = 0);
        ~Theme();
    
        QColor getClr();
        void setClr(QColor c);
    
    private:
        QColor clr;
    };
    

    class.cpp

    Theme::Theme(QWidget *parent)
        : QWidget(parent)
    {
    }
    
    Theme::~Theme()
    {
    
    }
    
    QColor Theme::getClr()
    {
        return clr;
    }
    
    void Theme::setClr(QColor c)
    {
        clr = c;
    }
    

    stylesheet.qss

    * {
        qproperty-clr: red;
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Are you sure your style sheet is applied ?

      Even if it comes from the resource, you should check that the file is opened properly.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Are you sure your style sheet is applied ?

        Even if it comes from the resource, you should check that the file is opened properly.

        R Offline
        R Offline
        RyuMake
        wrote on last edited by
        #3

        @SGaist Thank you,

        Style sheet is applied OK, I can modify anything with the QSS file and there is no error in console.

        mrjjM 1 Reply Last reply
        0
        • R RyuMake

          @SGaist Thank you,

          Style sheet is applied OK, I can modify anything with the QSS file and there is no error in console.

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

          @RyuMake
          Which part is not working?
          is void Theme::setClr(QColor c) called with red ?
          Anyway, i wondering if you need
          https://wiki.qt.io/Dynamic_Properties_and_Stylesheets
          unpolish();
          polish();

          R 1 Reply Last reply
          0
          • mrjjM mrjj

            @RyuMake
            Which part is not working?
            is void Theme::setClr(QColor c) called with red ?
            Anyway, i wondering if you need
            https://wiki.qt.io/Dynamic_Properties_and_Stylesheets
            unpolish();
            polish();

            R Offline
            R Offline
            RyuMake
            wrote on last edited by
            #5

            Hi @mrjj,
            Specifically it's this part that isn't working.

            p.setColor(QPalette::WindowText, theme->getClr);
            

            Though log gets spammed with "[widget] does not have a property named [var]".

            I'll look into polish.

            mrjjM 1 Reply Last reply
            0
            • R RyuMake

              Hi @mrjj,
              Specifically it's this part that isn't working.

              p.setColor(QPalette::WindowText, theme->getClr);
              

              Though log gets spammed with "[widget] does not have a property named [var]".

              I'll look into polish.

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

              @RyuMake
              Hi
              But is theme affected by stylesheet?
              You seems to give it no parent so even you set stylesheet to qApp
              i would suspect :getClr() to return default value for clr since its not under stylesheet effect.
              As far as it reads. Could be wrong :)

              R 1 Reply Last reply
              0
              • mrjjM mrjj

                @RyuMake
                Hi
                But is theme affected by stylesheet?
                You seems to give it no parent so even you set stylesheet to qApp
                i would suspect :getClr() to return default value for clr since its not under stylesheet effect.
                As far as it reads. Could be wrong :)

                R Offline
                R Offline
                RyuMake
                wrote on last edited by
                #7

                @mrjj
                Hi, yes theme is affected by style sheet, if I make a random paintEvent (draw a line across the screen) I can change its color from within the style sheet using the qproperty.

                Basically the idea is to set a color value to qApp style (Fusion) by external style sheet. I wonder if it's not possible? I get no errors, program runs perfectly OK, it just displays black though.

                mrjjM 1 Reply Last reply
                0
                • R RyuMake

                  @mrjj
                  Hi, yes theme is affected by style sheet, if I make a random paintEvent (draw a line across the screen) I can change its color from within the style sheet using the qproperty.

                  Basically the idea is to set a color value to qApp style (Fusion) by external style sheet. I wonder if it's not possible? I get no errors, program runs perfectly OK, it just displays black though.

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

                  @RyuMake
                  So if you use a theme instance in a paint event
                  The color set by stylesheet is red ?
                  Ok. just looked odd. I thought it might not be affected but seems it is then.

                  But getClr IS called ? for your instance?

                  1 Reply Last reply
                  1
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    You are getting all these messages because your style sheet applies to all widgets and only Theme provides that property.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • R Offline
                      R Offline
                      RyuMake
                      wrote on last edited by
                      #10

                      @SGaist You're right, setting it to a specific widget cleans the log. I was using global to see if I could get this to work.

                      @mrjj Yes, getClr is called.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        RyuMake
                        wrote on last edited by
                        #11

                        I set some debug messages to my getter and setter, I get messages in the log for both. If I break the style sheet syntax I'll only get the setter message and an error saying style sheet could not be parsed, so it's definitely working, it just won't work with setStyle QPalette. I wonder if it can't be done in main()?

                        My instance:

                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                        
                            QApplication::setStyle(QStyleFactory::create("Fusion"));
                        
                            Theme themeObject;
                            Theme *themePointer = &themeObject;
                        
                            QPalette p;
                            p = qApp->palette();
                            p.setColor(QPalette::Window, themePointer->bgColor());    // <--- HERE
                            qApp->setPalette(p);
                        
                            return a.exec();
                        }
                        
                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          What OS are you on ?

                          Depending on the style used, the palette or part of it can be ignored by it.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          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