Results 1 to 4 of 4

Thread: QPalette and stylesheet

  1. #1
    Join Date
    Apr 2012
    Location
    France
    Posts
    13

    Default Re: QPalette and stylesheet

    Hi all

    I was wondering if there is any way to retrieve colors set by the stylesheet for different states of a listview item.
    I am developing listviews and use an item delegate to paint my items.
    Then I need to use brushes to paint the items according to their states and I wanted to retrieve the color set by my css file dynamically without having to use hard coded colors.

    Any suggestions ?

    thanks


    Added after 14 minutes:


    here is the css for the listview items

    /* ----------------------------------------------------------------------------*/
    /* QListView */

    QListView
    {
    margin: 0px;
    border: 0px;
    padding: 0px;
    background-color: none;
    }

    /* NORMAL STATE */
    QListView::item
    {
    margin: 0px;
    border: 0px;
    padding: 0px;
    background-color: rgb(249, 249, 246);
    }

    /* SELECTED STATE */
    QListView::item:selected
    {
    background-color: rgb(235, 235, 234);
    }

    /* FOCUSED (or selected) STATE */
    QListView::item:focus
    {
    background-color: rgb(235, 235, 234);
    }

    /* FOCUSED but hovering over STATE */
    QListView::item:focus:hover
    {
    background-color: rgb(234, 234, 233);
    }

    /* SELECTED but hovering over STATE */
    QListView::item:selected:hover
    {
    background-color: rgb(234, 234, 233);
    }

    /* HOVER only STATE */
    QListView::item:hover
    {
    background-color: rgb(245, 245, 242);
    }
    Last edited by Zalwou06; 21st May 2012 at 13:16.

  2. #2
    Join Date
    Sep 2011
    Location
    Portugal
    Posts
    25
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QPalette and stylesheet

    This is how I usually do it. You must set a Q_PROPERTY for each property you want, and then get those values in the delegate.

    Your custom QListView class would be something like this:

    Qt Code:
    1. class MyListView : public QListView
    2. {
    3. Q_OBJECT
    4. // Color properties
    5. Q_PROPERTY(QColor normalItemColor READ normalItemColor WRITE setNormalItemColor)
    6. Q_PROPERTY(QColor selectedItemColor READ selectedItemColor WRITE setSelectedItemColor)
    7.  
    8. public:
    9. MyListView(QWidget *parent = 0);
    10.  
    11. public:
    12. void setNormalItemColor(const QColor &normalItemColor) { m_normalItemColor = normalItemColor; }
    13. QColor normalItemColor() { return m_normalItemColor; }
    14.  
    15. void setSelectedItemColor(const QColor &selectedItemColor) { m_selectedItemColor = selectedItemColor; }
    16. QColor selectedItemColor() { return m_selectedItemColor; }
    17.  
    18. private:
    19. QColor m_normalItemColor;
    20. QColor m_selectedItemColor;
    21. }
    To copy to clipboard, switch view to plain text mode 

    Then, the MyListView stylesheet would be something like:

    Qt Code:
    1. qproperty-normalItemColor: #6C6F70;
    2. qproperty-selectedItemColor: #C2C2C2;
    3. }
    To copy to clipboard, switch view to plain text mode 

    Of course you must pass the MyListView object to the delegate and then get the property values on the paintEvent().

    You can read more about properties here: http://qt-project.org/doc/qt-4.8/properties.html
    I hope this helps.

  3. #3
    Join Date
    Apr 2012
    Location
    France
    Posts
    13

    Default Re: QPalette and stylesheet

    Thanks

    that could be a solution but I am using QuiLoader to load external UI files so I cannot load custom widgets.
    I could override QuiLoader and create the appropriate custom widget but I do not want to.

    I am searching for another solution to avoid using hard coded colors in my delegate and I am stiill searching in the Qt code how the css is read and how widgets are using these values...

    thanks anyway for you cool solution.

  4. #4
    Join Date
    Jul 2012
    Posts
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QPalette and stylesheet

    You can retrieve colors in code that were set in the stylesheet, however I don't think you can do it in the constructor as the style sheet may not have been read yet.

    I have been capturing them in the showEvent:

    Example:

    Qt Code:
    1. class MyWidget: public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyWidget(QWidget* par);
    7.  
    8. protected:
    9. void showEvent(QShowEvent* evt);
    10.  
    11. private:
    12. QColor fgColor;
    13. QColor bgColor;
    14. QColor altBGColor;
    15. }
    16.  
    17. void MyWidget::showEvent(QShowEvent* evt)
    18. {
    19. static bool first = true; // Prevent constant re-querying
    20. if (first)
    21. {
    22. QPalette pal = palette();
    23. fgColor = pal.color(QPalette::Active, QPalette::Text); // color in stylesheet
    24. bgColor = pal.color(QPalette::Active, QPalette::Window); // background-color in stylesheet
    25. altBGColor = pal.color(QPalette::Active, QPalette::AlternateBase); //alternate-background-color in stylesheet
    26. // NOTE: Highlighted foreground and background are also available
    27. first = false;
    28. }
    29. // ...
    30. }
    To copy to clipboard, switch view to plain text mode 
    That's it... Your style Sheet might look like this for MyWidget:

    Qt Code:
    1. MyWidget
    2. {
    3. color: #ffffff;
    4. background-color: #000000;
    5. alternate-background-color: #200000;
    6. }
    To copy to clipboard, switch view to plain text mode 

    I hope this helps.
    Last edited by high_flyer; 12th July 2012 at 09:50. Reason: code tags

Similar Threads

  1. QPalette on PushButton
    By wagmare in forum Qt Programming
    Replies: 3
    Last Post: 30th January 2009, 13:44
  2. QPalette in rgb
    By tommy in forum Qt Programming
    Replies: 2
    Last Post: 15th January 2008, 10:21
  3. QPalette Coordinating
    By ToddAtWSU in forum Qt Programming
    Replies: 4
    Last Post: 16th February 2007, 18:42
  4. QPalette help pls
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th September 2006, 14:41
  5. QPalette help pls
    By munna in forum Qt Programming
    Replies: 7
    Last Post: 24th July 2006, 19:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.