PDA

View Full Version : QPalette and stylesheet



Zalwou06
21st May 2012, 13:16
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);
}

tferreira
21st May 2012, 15:23
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:



class MyListView : public QListView
{
Q_OBJECT
// Color properties
Q_PROPERTY(QColor normalItemColor READ normalItemColor WRITE setNormalItemColor)
Q_PROPERTY(QColor selectedItemColor READ selectedItemColor WRITE setSelectedItemColor)

public:
MyListView(QWidget *parent = 0);

public:
void setNormalItemColor(const QColor &normalItemColor) { m_normalItemColor = normalItemColor; }
QColor normalItemColor() { return m_normalItemColor; }

void setSelectedItemColor(const QColor &selectedItemColor) { m_selectedItemColor = selectedItemColor; }
QColor selectedItemColor() { return m_selectedItemColor; }

private:
QColor m_normalItemColor;
QColor m_selectedItemColor;
}


Then, the MyListView stylesheet would be something like:



QListView {
qproperty-normalItemColor: #6C6F70;
qproperty-selectedItemColor: #C2C2C2;
}


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.

Zalwou06
22nd May 2012, 09:12
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.

whawk640
11th July 2012, 21:40
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:


class MyWidget: public QWidget
{
Q_OBJECT

public:
MyWidget(QWidget* par);

protected:
void showEvent(QShowEvent* evt);

private:
QColor fgColor;
QColor bgColor;
QColor altBGColor;
}

void MyWidget::showEvent(QShowEvent* evt)
{
static bool first = true; // Prevent constant re-querying
if (first)
{
QPalette pal = palette();
fgColor = pal.color(QPalette::Active, QPalette::Text); // color in stylesheet
bgColor = pal.color(QPalette::Active, QPalette::Window); // background-color in stylesheet
altBGColor = pal.color(QPalette::Active, QPalette::AlternateBase); //alternate-background-color in stylesheet
// NOTE: Highlighted foreground and background are also available
first = false;
}
// ...
}

That's it... Your style Sheet might look like this for MyWidget:


MyWidget
{
color: #ffffff;
background-color: #000000;
alternate-background-color: #200000;
}

I hope this helps.