PDA

View Full Version : QTreeView and spanning columns



Lykurg
18th October 2009, 17:44
Hi,

I'll have a model represented in a tree view with three levels of which the last level has n columns. I want that the first two levels are spanning over the n columns. Right now I am using QTreeView::setFirstColumnSpanned(). The "problem" is only that the model/view will chance very often and therefor I have to call setFirstColumnSpanned every time the view's data gets updated. Is there maybe an option - I have missed - in a custom model which does that automatically or is setFirstColumnSpanned the only way for it.

Sample code:

#include <QtGui>

class Delegate : public QItemDelegate
{
public:
Delegate(QObject *parent = 0);

protected:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QItemDelegate::paint(painter, option, index);
// highlight the "item" which should be spanned
painter->setBrush(QColor(200,200,200,50));
if(index.child(0,0).isValid())
painter->drawRect(option.rect);
}
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QStandardItemModel model(2, 2);
for (int a = 0; a < 2; ++a) {
QStandardItem *itemA = new QStandardItem(QString("A: %0").arg(a));
for (int b = 0; b < 2; ++b) {
QStandardItem *itemB = new QStandardItem(QString("A: %0; B: %1").arg(a).arg(b));
for (int c = 0; c < 2; ++c) {
for (int d = 0; d < 4; ++d) {
QStandardItem *itemC = new QStandardItem(QString("(%0;%1) C: %2/%3").arg(a).arg(b).arg(c).arg(d));
itemB->setChild(c,d,itemC);
}
}
itemA->setChild(b,0,itemB);
}
model.setItem(a, itemA);
}
QTreeView tree;
tree.setModel(&model);
tree.setItemDelegate(new Delegate(&a));

// this I want to avoid because normally the displayed data is much more...
tree.setFirstColumnSpanned(0,model.index(-1,0),true);
tree.setFirstColumnSpanned(1,model.index(-1,0),true);
tree.setFirstColumnSpanned(0,model.index(0,0),true );
tree.setFirstColumnSpanned(1,model.index(0,0),true );
tree.setFirstColumnSpanned(0,model.index(1,0),true );
tree.setFirstColumnSpanned(1,model.index(1,0),true );

tree.show();
tree.expandAll();
return a.exec();
}

Lykurg
8th August 2011, 13:53
almost two years later. BUMP and HELP! :D

wysota
8th August 2011, 17:20
Maybe you're approaching the problem from the wrong end? How about subclassing QTreeView and doing the spanning there? As a last resort you can connect to the signals from the model emitted when the model changes and enable spanning based on some properties (e.g. custom role) of the model.

TorstenG
13th February 2013, 20:41
Well, I haven't found a good solution, but a way to to it.

I call setFirstColumnSpanned() directly from my model from the data() method when this item is accessed.
Of course, the model needs to know its view ... and this causes a bit headache but it works for me.

Shikha
22nd September 2017, 12:58
Hi, can you please post here how did you call the function setFirstColumnSpanned from the data() function. I am having similar problem but didn't find any solution to this.