PDA

View Full Version : how to show vertical lines between colums



thomasjoy
22nd August 2007, 12:24
hi all
i am using Qt4.1 on Mac OS

the idea is to showing the vertical lines between colums of QTreeWidget
as i have 4 colums in treewidget.
how it can be done???

and how data coming in all colums should be aligned squarely or in proper manner???


thanks in advance

jpn
22nd August 2007, 12:39
I don't think there's any built-in way for doing that. What you can do is to reimplement paintEvent() and paint them yourself. The header provides information about column (section) positions and sizes.

thomasjoy
22nd August 2007, 12:54
can you please explai how it can be done in simple example with no. of items are there in TreeWidget

jpn
22nd August 2007, 14:18
Alright, here's an example:


#include <QtGui>

class TreeWidget : public QTreeWidget
{
public:
TreeWidget(QWidget* parent = 0) : QTreeWidget(parent)
{
setColumnCount(5);
}

protected:
void paintEvent(QPaintEvent* event)
{
QTreeWidget::paintEvent(event);
QPainter painter(viewport());
for (int i = 0; i < header()->count(); ++i)
{
// draw only visible sections starting from second column
if (header()->isSectionHidden(i) || header()->visualIndex(i) <= 0)
continue;

// position mapped to viewport
int pos = header()->sectionViewportPosition(i) - 1;
if (pos > 0)
painter.drawLine(QPoint(pos, 0), QPoint(pos, height()));
}
}

void scrollContentsBy(int dx, int dy)
{
QTreeWidget::scrollContentsBy(dx, dy);
// make sure lines get updated even if the view is empty
viewport()->update();
}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
TreeWidget tree;
tree.show();
return app.exec();
}

You could use for example QStyle::SH_Table_GridLineColor as pen color.

thomasjoy
22nd August 2007, 15:03
thanks for such a nice example

but i dont have to use qpen or Qpaint properties...

can we disable the streching of treewidget's header
i.e header should be of fixed width