QStyle vs Qt::BackgroundColorRole
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..
Code:
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..
Code:
//TReeStyle COde
void TreeStyle
::drawPrimitive(PrimitiveElement element,
const QStyleOption* option,
{
if (element == PE_PanelItemViewRow || element == PE_PanelItemViewItem)
{
painter->fillRect(option->rect, option->palette.highlight());
}
.......................
.......................
Code:
//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 ?
Re: QStyle vs Qt::BackgroundColorRole
sure you can. sounds like a bug in your style implementation.
Re: QStyle vs Qt::BackgroundColorRole
Oh....
this is my code. Please helpme for fix it. Btw, I use this code for testing only...
treestyle.cpp
Code:
#include <QPainter>
#include <QStyleOption>
#include <QWidget>
#include "treestyle.h"
TreeStyle::TreeStyle() : QProxyStyle()
{
}
TreeStyle::~TreeStyle()
{
}
void TreeStyle
::drawPrimitive(PrimitiveElement element,
const QStyleOption* option,
{
if (element == PE_PanelItemViewRow || element == PE_PanelItemViewItem)
{
painter->fillRect(option->rect, option->palette.highlight());
}
else
{
QProxyStyle::drawPrimitive(element, option, painter, widget);
}
}
mymodel.cpp
Code:
#include "mymodel.h"
{
}
{
if (role == Qt::BackgroundColorRole)
{
return qVariantFromValue
(QColor(255,
186,
186));
}
}
dialog.cpp
Code:
model=new MyModel();
model->setColumnCount(2);
..........
............
ui->treeView->setModel(model);
ui->treeView->setAlternatingRowColors(true);
ui->treeView->header()->setMovable(false);
ui->treeView->setStyle(new TreeStyle());
Re: QStyle vs Qt::BackgroundColorRole
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
Re: QStyle vs Qt::BackgroundColorRole
The QColor() constructor doesn't return anything so qVariantFromValue() won't work. Try this instead:
return QVariant( QColor(255, 186, 186) );