+ make PropertyEditor class ready to be customized with Qt style sheet

This commit is contained in:
wmayer 2016-02-20 16:00:35 +01:00
parent 4928624d14
commit 60e0c447ca
3 changed files with 64 additions and 4 deletions

View File

@ -49,6 +49,10 @@ PropertyEditor::PropertyEditor(QWidget *parent)
setAlternatingRowColors(true);
setRootIsDecorated(true);
QStyleOptionViewItem opt = viewOptions();
this->background = opt.palette.dark();
this->groupColor = opt.palette.color(QPalette::BrightText);
}
PropertyEditor::~PropertyEditor()
@ -65,6 +69,26 @@ bool PropertyEditor::isAutomaticDocumentUpdate(bool) const
return autoupdate;
}
QBrush PropertyEditor::groupBackground() const
{
return this->background;
}
void PropertyEditor::setGroupBackground(const QBrush& c)
{
this->background = c;
}
QColor PropertyEditor::groupTextColor() const
{
return this->groupColor;
}
void PropertyEditor::setGroupTextColor(const QColor& c)
{
this->groupColor = c;
}
QStyleOptionViewItem PropertyEditor::viewOptions() const
{
QStyleOptionViewItem option = QTreeView::viewOptions();
@ -114,7 +138,7 @@ void PropertyEditor::drawBranches(QPainter *painter, const QRect &rect, const QM
QStyleOptionViewItem opt = viewOptions();
PropertyItem *property = static_cast<PropertyItem*>(index.internalPointer());
if (property && property->isSeparator()) {
painter->fillRect(rect, opt.palette.dark());
painter->fillRect(rect, this->background);
//} else if (selectionModel()->isSelected(index)) {
// painter->fillRect(rect, opt.palette.brush(QPalette::Highlight));
}

View File

@ -41,10 +41,26 @@ namespace Gui {
namespace PropertyEditor {
class PropertyModel;
/*!
Put this into the .qss file after Gui--PropertyEditor--PropertyEditor
Gui--PropertyEditor--PropertyEditor
{
qproperty-groupBackground: gray;
qproperty-groupTextColor: white;
}
See also: https://man42.net/blog/2011/09/qt-4-7-modify-a-custom-q_property-with-a-qt-style-sheet/
*/
class PropertyEditor : public QTreeView
{
Q_OBJECT
Q_PROPERTY(QBrush groupBackground READ groupBackground WRITE setGroupBackground DESIGNABLE true SCRIPTABLE true)
Q_PROPERTY(QColor groupTextColor READ groupTextColor WRITE setGroupTextColor DESIGNABLE true SCRIPTABLE true)
public:
PropertyEditor(QWidget *parent = 0);
~PropertyEditor();
@ -57,6 +73,11 @@ public:
void setAutomaticDocumentUpdate(bool);
bool isAutomaticDocumentUpdate(bool) const;
QBrush groupBackground() const;
void setGroupBackground(const QBrush& c);
QColor groupTextColor() const;
void setGroupTextColor(const QColor& c);
protected:
virtual void closeEditor (QWidget * editor, QAbstractItemDelegate::EndEditHint hint);
virtual void commitData (QWidget * editor);
@ -72,6 +93,8 @@ private:
bool autoupdate;
bool committing;
bool delaybuild;
QColor groupColor;
QBrush background;
};
} //namespace PropertyEditor

View File

@ -58,7 +58,14 @@ void PropertyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
PropertyItem *property = static_cast<PropertyItem*>(index.internalPointer());
if (property && property->isSeparator()) {
option.palette.setColor(QPalette::Text, option.palette.color(QPalette::BrightText));
QColor color = option.palette.color(QPalette::BrightText);
QObject* par = parent();
if (par) {
QVariant value = par->property("groupTextColor");
if (value.canConvert<QColor>())
color = value.value<QColor>();
}
option.palette.setColor(QPalette::Text, color);
option.font.setBold(true);
option.state &= ~QStyle::State_Selected;
}
@ -70,8 +77,14 @@ void PropertyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
option.state &= ~QStyle::State_HasFocus;
if (property && property->isSeparator()) {
QBrush bg = option.palette.dark();
painter->fillRect(option.rect, bg);
QBrush brush = option.palette.dark();
QObject* par = parent();
if (par) {
QVariant value = par->property("groupBackground");
if (value.canConvert<QBrush>())
brush = value.value<QBrush>();
}
painter->fillRect(option.rect, brush);
}
QPen savedPen = painter->pen();