Expose custom parameters to QSS
-
Hi. I have custom widget, like CustomWidget : public QWidget
I have QTreeView inside in this CustomWidget (as a part of my custom widget), QTreeView's item delegate invokes data() method, where I return one of 2 colors: black for regular text, and red color, if there is an error.
Question is, how can I expose such colors to QSS in some way?
like CustomWidget::default { color: black; } and CustomWidget::error { color: red; }?... else if (role == Qt::TextColorRole) { QColor color(Qt::red); //this will produce red text in my widget's tree items if (item->GetParent()->GetType() == TreeItem::k_typeGroup) color = Qt::blue; bool visible = IsVisible(); QColor c = visible ? Qt::gray : Qt::lightGray; return color; }
Thanks in advance
-
@b2soft
create 2 QColor properties for your custom widget. Then in your model retun those properties and use this QSS:CustomWidget { qproperty-default: "#FFFFFF"; qproperty-error: "red"; }
-
@raven-worx ,
I create Q_PROPERTY for such colors in this way:class BrowserModel : public QAbstractItemModel { Q_OBJECT public: Q_PROPERTY( QColor textColor MEMBER m_textColor ); QColor m_textColor{ Qt::black }; ... }
Note, that BrowserModel is a member of BrowserWnd QDockWidget, but such strings have no effect:
BrowserModel { qproperty-textColor: #ff0000; }
-
@b2soft
as in my example: pass the value as string orrgb(255, 0, 0)
-
@raven-worx
Did not help. I have no idea why. Assume ,that code is kinda similar to this question's code: https://stackoverflow.com/questions/30170922/how-can-i-get-the-background-color-returned-by-model-take-precedence-over-the-stI have a custom widget. That contains QTreeView and for this QTreeView BrowserModel is set.
I want to customize BrowserModel ::data() returning colors using QSS -
@b2soft said in Expose custom parameters to QSS:
in order to make something like this to work:BrowserModel
{
qproperty-textColor: #ff0000;
}you need to specify
Q_OBJECT
in your custom class. At least in the code snippet you've posted this is not the case. -
@raven-worx edited, sorry (actual class is pretty big, missed the Q_OBJECT in the topic)
-
I want to customize BrowserModel ::data() returning colors using QSS
@raven-worx knows much more than I, but I don't get this.
role == Qt::TextColorRole
will want to return aQBrush
(http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum). How is that linked to returning a string to use for a name in a stylesheet? :confused: