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);
}
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:
Code:
{
Q_OBJECT
// Color properties
Q_PROPERTY(QColor normalItemColor READ normalItemColor WRITE setNormalItemColor
) Q_PROPERTY(QColor selectedItemColor READ selectedItemColor WRITE setSelectedItemColor
)
public:
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:
}
Then, the MyListView stylesheet would be something like:
Code:
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.
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.
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:
Code:
{
Q_OBJECT
public:
protected:
private:
}
{
static bool first = true; // Prevent constant re-querying
if (first)
{
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:
Code:
MyWidget
{
color: #ffffff;
background-color: #000000;
alternate-background-color: #200000;
}
I hope this helps.