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());
model=new MyModel();
ui->treeView->setModel(model);
ui->treeView->setStyle(new TreeStyle());
To copy to clipboard, switch view to plain text mode
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,
{
if (element == PE_PanelItemViewRow || element == PE_PanelItemViewItem)
{
painter->fillRect(option->rect, option->palette.highlight());
}
.......................
.......................
//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());
}
.......................
.......................
To copy to clipboard, switch view to plain text mode
//MyModel Code
if (role == Qt::BackgroundColorRole)
{
.......................
.......................
return qVariantFromValue
(QColor(255,
186,
186));
}
//MyModel Code
if (role == Qt::BackgroundColorRole)
{
.......................
.......................
return qVariantFromValue(QColor(255, 186, 186));
}
To copy to clipboard, switch view to plain text mode
This is normal ? I can't use booth QStyle and Model for QTreeView ?
Bookmarks