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();
}
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();
}