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

How to get color of a QLabel.setObjectName("special_color") from palette?



  • Is there a way to get it without regexping styleSheet?

    label = QLabel()
    # color of normal QLable (white)
    color = label.palette().color(QPalette.Text)
    
    label.setObjectName("special_color")
    #color = ?
    

    styleSheet

    QLabel {
    	color: white;
    }
    QLabel#special_color:hover {
    	color: green;
    }
    

  • Moderators

    @porsol
    The way is the same is the same. After setting the object-name the widget needs to be polished though, so the stylesheet is reapplied. In C++ the following can be done:

    widget->style()->polish(widget);
    


  • @raven-worx
    Basically I write my own QLabel, because native QLabel doesn't support things I need. So before drawing text, I wanted to get its color(special_color) from stylesheet.
    Can I get it through QPalette?
    It must be stored somewhere in memory but the actual css file, no?


Log in to reply