Results 1 to 2 of 2

Thread: QTreeView and Column/Row Gridlines

  1. #1
    Join Date
    Jul 2010
    Location
    United States
    Posts
    13
    Thanks
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default QTreeView and Column/Row Gridlines

    I have seen this question asked before, but I did not quite see an answer that cleared it up for me. A QTreeView has rows and columns. Is there a way to draw gridlines so that each row and column is clearly outlined or just vertical lines to separate columns? I looked at Qt Designer and their Property Editor does this. Does creating a feature like this require reimplementing the drawRow function or is there a straight forward way to do this?

    Thanks in advance

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTreeView and Column/Row Gridlines

    You could use a delegate
    Qt Code:
    1. class GridDelegate : public QStyledItemDelegate
    2. {
    3. public:
    4. explicit GridDelegate(QObject * parent = 0) : QStyledItemDelegate(parent) { }
    5.  
    6. void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    7. {
    8. painter->save();
    9. painter->setPen(QColor(Qt::black));
    10. painter->drawRect(option.rect);
    11. painter->restore();
    12.  
    13. QStyledItemDelegate::paint(painter, option, index);
    14. }
    15. };
    16.  
    17. int main(int argc, char ** argv)
    18. {
    19. QApplication app(argc, argv);
    20.  
    21. model.setRowCount(5);
    22. model.setColumnCount(5);
    23.  
    24. for(int r = 0; r < model.rowCount(); r++)
    25. for(int c = 0; c < model.columnCount(); c++)
    26. model.setItem(r, c, new QStandardItem("Item"));
    27.  
    28. QTreeView treeView;
    29. treeView.setModel(&model);
    30. treeView.setItemDelegate(new GridDelegate(&treeView));
    31. treeView.show();
    32.  
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    smhall316 (12th July 2013)

Similar Threads

  1. [Qt4 - QTableWidget] thickness gridlines
    By Boy in forum Qt Programming
    Replies: 6
    Last Post: 28th May 2013, 01:38
  2. QTreeView column resize problem
    By Qiieha in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2012, 15:22
  3. QTreeview last Column visibility
    By kiran p in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2011, 11:25
  4. Fixed Column in QTreeview
    By ormonde in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 08:49
  5. No delegate for 1 column in QTreeView
    By mace in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2007, 11:55

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.