How to let QTreeView colour the entire row?
-
I'm using QTreeView to display a multi-columns model. But when I select a row, QTreeView won't colour the entire row. The spacing at the end of the row has no colour. When I set colour for QPalette::AlternateBase, I find out that this is still not colour the left spacing of each row. How to colour the entire row in a QTreeView?
-
The QtreeView gets it's information (also colour) from the model (data() function in model).
Setup the colour there? Or what you might see is the spacing of the containing layout? A picture might give some more clues. -
I only set colour at QTreeView. And I think QTreeView will not get colour information from the model.
See the qtreevew.cpp. I find out that QTreeView only draws the rowCount() items and ignore those things after last section. I hope to paint those pixels.
[quote author="Jeroentje@home" date="1401972805"]The QtreeView gets it's information (also colour) from the model (data() function in model).
Setup the colour there? Or what you might see is the spacing of the containing layout? A picture might give some more clues.[/quote] -
Hi,
as model-view is implemented in Qt I was pretty sure, that very type of view would get its colour information from the model. That's what the "Qt::BackgroundRole":http://qt-project.org/doc/qt-4.8/qt.html#ItemDataRole-enum is for, right?
Could you elaborate more on why you think the model is not the place to solve your poeblem?!? I'm not really getting it.
Best Soraltan
-
First, model only contains the data for all kinds of roles. But the color is what view should knows. The model shouldn't owned any property of how to display a data.
Second, QTreeView has this property:
alternatingRowColors : bool
In the documentation, it says clearly:This property holds whether to draw the background using alternating colors.
If this property is true, the item background will be drawn using QPalette::Base and QPalette::AlternateBase; otherwise the background will be drawn using the QPalette::Base color.As you can see, these color is a part of the QTreeView. I think the QPalette class is right designed for these things.
Please look at the defination of the QPalette::AlternateBase.Used as the alternate background color in views with alternating row colors (see QAbstractItemView::setAlternatingRowColors()).
Third, If you still think the colour of a QTreeView is read from a model, please look at the source code of QTreeView:
(at in src/gui/itemviews/qtreeview.cpp)
Line 1677:
o.backgroundColor = option.palette.color(cg, d->selectionModel->isSelected(index)
? QPalette::Highlight : QPalette::Background);Line 1691:
style()->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter);These codes are definately shows that the QTreeView are using the QPalette class right in the view. Not get any data from the model.
Fourth, In drawRow() function of the QTreeView.
(at in src/gui/itemviews/qtreeview.cpp)Line 1522:
int columnCount = header->count();
const bool hoverRow = selectionBehavior() == QAbstractItemView::SelectRows
&& index.parent() == hover.parent()
&& index.row() == hover.row();/* 'left' and 'right' are the left-most and right-most visible visual indices. Compute the first visible logical indices before and after the left and right. We will use these values to determine the QStyleOptionViewItemV4::viewItemPosition. */ int logicalIndexBeforeLeft = -1, logicalIndexAfterRight = -1; for (int visualIndex = left - 1; visualIndex >= 0; --visualIndex) { int logicalIndex = header->logicalIndex(visualIndex); if (!header->isSectionHidden(logicalIndex)) { logicalIndexBeforeLeft = logicalIndex; break; } } QVector<int> logicalIndices; // vector of currently visibly logical indices for (int visualIndex = left; visualIndex < columnCount; ++visualIndex) { int logicalIndex = header->logicalIndex(visualIndex); if (!header->isSectionHidden(logicalIndex)) { if (visualIndex > right) { logicalIndexAfterRight = logicalIndex; break; } logicalIndices.append(logicalIndex); } } for (int currentLogicalSection = 0; currentLogicalSection < logicalIndices.count(); ++currentLogicalSection) {
QTreeView only process the graphics up to visible header count, so it will not process the space righter than the right-most visible items. This has nothing relate to the model.
So that's why I think the model is not the place to solve my problem. You can read the code here: https://qt.gitorious.org/qt/qt/source/5fe6a7457033b183d8cc3861fe8593338ad3385b:src/gui/itemviews/qtreeview.cpp#L1539-1544
[quote author="Soraltan" date="1401980386"]Hi,
as model-view is implemented in Qt I was pretty sure, that very type of view would get its colour information from the model. That's what the "Qt::BackgroundRole":http://qt-project.org/doc/qt-4.8/qt.html#ItemDataRole-enum is for, right?
Could you elaborate more on why you think the model is not the place to solve your poeblem?!? I'm not really getting it.
Best Soraltan[/quote]
-
I have tried to write a new style, and reset the width to the widget's width.
But I'm using a alpha channel in the QTreeview, the colour is very strange when several colour comes together :(