PDA

View Full Version : QStyle vs Qt::BackgroundColorRole



wirasto
22nd January 2010, 17:51
I just make a my custom style. In my QStyle, I fillrec itemviewrow on drawPrimitive function. Oh, yes I use this style just for my QTreeView..



model=new MyModel();
ui->treeView->setModel(model);
ui->treeView->setStyle(new TreeStyle());


In MyModel I tried set backgroud color for a row with Qt::BackgroundColorRole. But I don't understand why not work. If I remove setStyle from my treeView, Qt::BackgroundColorRole will work..



//TReeStyle COde
void TreeStyle::drawPrimitive(PrimitiveElement element, const QStyleOption* option,
QPainter* painter, const QWidget* widget) const
{
if (element == PE_PanelItemViewRow || element == PE_PanelItemViewItem)
{
painter->fillRect(option->rect, option->palette.highlight());
}
.......................
.......................





//MyModel Code
if (role == Qt::BackgroundColorRole)
{
.......................
.......................
return qVariantFromValue(QColor(255, 186, 186));

}


This is normal ? I can't use booth QStyle and Model for QTreeView ?

caduel
22nd January 2010, 18:13
sure you can. sounds like a bug in your style implementation.

wirasto
22nd January 2010, 18:39
Oh....
this is my code. Please helpme for fix it. Btw, I use this code for testing only...


treestyle.cpp


#include <QPainter>
#include <QStyleOption>
#include <QWidget>
#include "treestyle.h"

TreeStyle::TreeStyle() : QProxyStyle()
{
}

TreeStyle::~TreeStyle()
{
}

void TreeStyle::drawPrimitive(PrimitiveElement element, const QStyleOption* option,
QPainter* painter, const QWidget* widget) const
{
if (element == PE_PanelItemViewRow || element == PE_PanelItemViewItem)
{
painter->fillRect(option->rect, option->palette.highlight());
}
else
{
QProxyStyle::drawPrimitive(element, option, painter, widget);
}
}



mymodel.cpp


#include "mymodel.h"

MyModel::MyModel(QObject *parent)
:QStandardItemModel(parent)
{
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::BackgroundColorRole)
{
return qVariantFromValue(QColor(255, 186, 186));
}
return QStandardItemModel::data(index, role);
}



dialog.cpp


model=new MyModel();
model->setColumnCount(2);
..........
............
model->setHorizontalHeaderItem(0, new QStandardItem("Type"));
model->setHorizontalHeaderItem(1, new QStandardItem("Uang"));

ui->treeView->setModel(model);
ui->treeView->setAlternatingRowColors(true);
ui->treeView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
ui->treeView->header()->setMovable(false);
ui->treeView->setStyle(new TreeStyle());

caduel
23rd January 2010, 09:40
well, I don't know what you want your style to do, so hard to say what is wrong with it.

A fix might be to first call the base class (or proxy style) and let it paint and then paint your stuff over it.

(You can see in Qt's QCommonStyle implementation (the source is available, after all) that the code you override (and no longer execute) is responsible for painting the background in alternating row colors.
see: src/gui/styles/qcommonstyle.cpp

HTH

Hawkeye64
10th April 2015, 16:23
The QColor() constructor doesn't return anything so qVariantFromValue() won't work. Try this instead:

return QVariant( QColor(255, 186, 186) );