Results 1 to 3 of 3

Thread: QTreeView children with multiple coulms

  1. #1
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default QTreeView children with multiple coulms

    How can I have children that have a second column of data? I am creating a table of contents and the parent items will properly show the second column with the page number but children do not. I have tried making the child page number a child of the parent page number. I read somewhere that children only get 1 column. Is there a way around this? I tried using HTML so that the title was aligned left and the page was aligned right in 1 column for both parent and child. However it seems the QTextDocument (inserted using QStyledItemDelegate) does not want to use the full width of the QTreeView (all of the text is together). Also, the QTextDocument does not highlight when a row is selected in the QTreeView.

    Any Suggestions?


    Qt Code:
    1. QString data = QString::fromUtf8(title) + "<span style=font-weight:bold;text-align:right;float:right>" + QString::number(page) + "</span>";
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void StyledItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    2. QStyleOptionViewItemV4 options = option;
    3. initStyleOption(&options, index);
    4.  
    5. painter->save();
    6. qDebug() << option.rect.width();
    7. doc.setTextWidth(option.rect.width());
    8. doc.setHtml(index.data().toString());
    9. //painter->save();
    10. painter->translate(option.rect.topLeft());
    11. doc.drawContents(painter,QRect(QPoint(0,0),option.rect.size()));
    12. painter->restore();
    13. }
    14.  
    15. QSize StyledItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
    16. QStyleOptionViewItemV4 options = option;
    17. initStyleOption(&options, index);
    18.  
    19. doc.setHtml(options.text);
    20. doc.setTextWidth(options.rect.width());
    21.  
    22. QSize size = QStyledItemDelegate::sizeHint(option, index);
    23. return QSize(size.width() > doc.idealWidth() ? size.width() : doc.idealWidth(),
    24. size.height() > doc.size().height() ? size.height() : doc.size().height());
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTreeView children with multiple coulms

    Put the data in a second column in the model and let QTreeView do the work for you. Children can have more than one column.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv)
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. QStandardItem *parent0 = new QStandardItem("Parent 0");
    8. QStandardItem *col1 = new QStandardItem("1");
    9. model.appendRow(QList<QStandardItem *>() << parent0 << col1);
    10. // make some children
    11. for (int i = 0; i < 3; ++i) {
    12. QStandardItem *child0 = new QStandardItem(QString("Child %1").arg(i));
    13. QStandardItem *child1 = new QStandardItem(QString("%1").arg(i));
    14. parent0->appendRow(QList<QStandardItem *>() << child0 << child1);
    15. }
    16.  
    17. v.setModel(&model);
    18. v.show();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    maxpower (20th June 2012)

  4. #3
    Join Date
    Oct 2006
    Posts
    60
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Wink Re: QTreeView children with multiple coulms

    Thank you! I was trying to add the child's second column to the parent in the second column. Your way actually works!

    Qt Code:
    1. QStandardItem *titleItem = new QStandardItem(QString::fromUtf8(title));
    2. model->setItem(row,0,titleItem);
    3. QStandardItem *pageParentItem = new QStandardItem(QString::number(page));
    4. model->setItem(row,1,pageItem);
    5. .
    6. .
    7. .
    8. QStandardItem *titleItem = new QStandardItem(QString::fromUtf8(title));
    9. titleItem->setData(outline->dest.ld.gotor.page,33);
    10. titleParentItem->appendRow(titleItem);
    11. QStandardItem *pageItem = new QStandardItem(QString::number(page));
    12. pageParentItem->appendRow(pageItem);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Hide children of QStandardItem in QTreeView
    By Phlucious in forum Qt Programming
    Replies: 2
    Last Post: 18th June 2012, 20:48
  2. Hide Parent and show only children in QTreeView
    By y.s.bisht in forum Qt Programming
    Replies: 8
    Last Post: 19th January 2012, 09:51
  3. Replies: 0
    Last Post: 14th April 2010, 15:03
  4. QTreeView adding children to the view
    By Tux-Slack in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2007, 09:28
  5. QTreeView and QStandardModel : add children
    By Valheru in forum Newbie
    Replies: 7
    Last Post: 19th September 2006, 17:24

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.