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
Re: QTreeView and Column/Row Gridlines
You could use a delegate
Code:
class GridDelegate : public QStyledItemDelegate
{
public:
explicit GridDelegate
(QObject * parent
= 0) : QStyledItemDelegate
(parent
) { }
{
painter->save();
painter
->setPen
(QColor(Qt
::black));
painter->drawRect(option.rect);
painter->restore();
QStyledItemDelegate::paint(painter, option, index);
}
};
int main(int argc, char ** argv)
{
model.setRowCount(5);
model.setColumnCount(5);
for(int r = 0; r < model.rowCount(); r++)
for(int c = 0; c < model.columnCount(); c++)
treeView.setModel(&model);
treeView.setItemDelegate(new GridDelegate(&treeView));
treeView.show();
return app.exec();
}