Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Changing QPalette::WindowText by Q_PROPERTY by QSS



  • 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;
    }
    

  • Lifetime Qt Champion

    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.



  • @SGaist Thank you,

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


  • Lifetime Qt Champion

    @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();



  • 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.


  • Lifetime Qt Champion

    @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 :)



  • @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.


  • Lifetime Qt Champion

    @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?


  • Lifetime Qt Champion

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



  • @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.



  • 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();
    }
    

  • Lifetime Qt Champion

    What OS are you on ?

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


Log in to reply