Results 1 to 5 of 5

Thread: QTreeView and spanning columns

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default QTreeView and spanning columns

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTreeView and spanning columns

    almost two years later. BUMP and HELP!

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView and spanning columns

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView and spanning columns

    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.

  5. #5

    Default Re: QTreeView and spanning columns

    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.

Similar Threads

  1. Replies: 1
    Last Post: 4th July 2008, 18:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.