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:
Qt Code:
  1. #include <QtGui>
  2.  
  3. class Delegate : public QItemDelegate
  4. {
  5. public:
  6. Delegate(QObject *parent = 0);
  7.  
  8. protected:
  9. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  10. {
  11. QItemDelegate::paint(painter, option, index);
  12. // highlight the "item" which should be spanned
  13. painter->setBrush(QColor(200,200,200,50));
  14. if(index.child(0,0).isValid())
  15. painter->drawRect(option.rect);
  16. }
  17. };
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21. QApplication a(argc, argv);
  22.  
  23. QStandardItemModel model(2, 2);
  24. for (int a = 0; a < 2; ++a) {
  25. QStandardItem *itemA = new QStandardItem(QString("A: %0").arg(a));
  26. for (int b = 0; b < 2; ++b) {
  27. QStandardItem *itemB = new QStandardItem(QString("A: %0; B: %1").arg(a).arg(b));
  28. for (int c = 0; c < 2; ++c) {
  29. for (int d = 0; d < 4; ++d) {
  30. QStandardItem *itemC = new QStandardItem(QString("(%0;%1) C: %2/%3").arg(a).arg(b).arg(c).arg(d));
  31. itemB->setChild(c,d,itemC);
  32. }
  33. }
  34. itemA->setChild(b,0,itemB);
  35. }
  36. model.setItem(a, itemA);
  37. }
  38. QTreeView tree;
  39. tree.setModel(&model);
  40. tree.setItemDelegate(new Delegate(&a));
  41.  
  42. // this I want to avoid because normally the displayed data is much more...
  43. tree.setFirstColumnSpanned(0,model.index(-1,0),true);
  44. tree.setFirstColumnSpanned(1,model.index(-1,0),true);
  45. tree.setFirstColumnSpanned(0,model.index(0,0),true);
  46. tree.setFirstColumnSpanned(1,model.index(0,0),true);
  47. tree.setFirstColumnSpanned(0,model.index(1,0),true);
  48. tree.setFirstColumnSpanned(1,model.index(1,0),true);
  49.  
  50. tree.show();
  51. tree.expandAll();
  52. return a.exec();
  53. }
To copy to clipboard, switch view to plain text mode