B
B
becks2014-04-01 16:03:01
Qt
becks, 2014-04-01 16:03:01

Is it possible to implement this on QTreeView and QStandardItemModel?

I made a craft on QTreeView and QStandardItemModel:
ff783edb0f1045a184e7b7278328a702.jpg
If the correlation between countries is briefly shown by some parameter (for example, the value of trade relations). The larger this parameter, the brighter the color in the cell. When you select a cell, the corresponding countries are highlighted. Everything seems to be fine, but I can't make the remaining "little things":
1) Colored cells make everything the same size.
I tried to do this in the data method, it did not help:

if (role == Qt::SizeHintRole)
{
      if (isColorCell())
         return QSize(50, 50);
}

2) Cells with text should be stretched according to the size of the text, without changing the size of the corresponding colored cells (if possible).
3) Change color when selected (now standard windows). I handle the event of a mouse click on a colored cell and select all the colored ones that lie with it on the same row and column. The highlight color covers the cell's inner color. I would like to change the color of the selection to a less bright one and make it almost transparent.
I tried to do this in the view constructor, but the selection color did not change:
QPalette p = palette();
    p.setColor(QPalette::Highlight, QColor("#FFFFCC"));
    p.setColor(QPalette::HighlightedText, Qt::black);
    setPalette(p);

By the way, I select the cells like this:
selectionModel()->select(index, QItemSelectionModel::ClearAndSelect |
                                    QItemSelectionModel::Rows |
                                    QItemSelectionModel::Columns);

4) Make a white frame of a couple of pixels between the colored cells so that the colors do not merge.
I would be grateful for hints.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DancingOnWater, 2014-04-02
@DancingOnWater

1) The size is set in the header (if I don’t confuse anything) And this way you can see by the triggering of events who drives what
2) No way, you already need to change the viewer
3) Change the corresponding event handler

X
xseven, 2014-04-08
@xseven

You can do almost everything from what you want .
You need to inherit from QStyledItemDelegate
1. The size of the cell (the maximum desired) is issued from the delegate using the sizeHint method.
There are some subtleties when working with it, but if you add metaproperties to the data, you can change the size (but within reasonable limits )
2. You need to look at the selection in the paint delegate method
. For example, I did this

void myClass::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
...
//[In case of selection override colour to selection color except mean row]
    if(QStyle::State_Selected & option.state)
    {
        QColor selectedColor = QApplication::palette().color(QPalette::Highlight);
        cellBrush = QBrush(selectedColor);
    }

    //[Fill background]
    painter->fillRect(itemRect, cellBrush);
}

Well, etc.
It is desirable to read:
qt-project.org/doc/qt-4.8/model-view-programming.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question