Hello, I'm trying to paint a border around QTreeWidget. This QTreeWidget has only one column, and it is intended to look similar to this:



That is, I am using it just to paint a hierarchy, not to have a spreadsheet-like widget. There are no column headers but, unlike that screenshot, the only column is supposed to always have a width of 100%.

For such matter I'm using qDrawShadePanel inside the paintEvent of the QTreeWidget. It is roughly like this:

Qt Code:
  1. void
  2. MyQTreeWidget::paintEvent(QPaintEvent *event)
  3. {
  4. QPainter *p = new QPainter(viewport());
  5. qDrawShadePanel(p, 0, 0, viewport()->width(), viewport()->height(), palette(), true, 1, NULL);
  6. delete p;
  7.  
  8. QTreeWidget::paintEvent(event);
  9. }
To copy to clipboard, switch view to plain text mode 

I also have a custom item delegate with a sizeHint() that paints a coloured background for each item and its text using a QPainter inside its paint() function. This coloured background spans the whole column.

Even though it all mostly works, I have a problem: the background I paint for each item overlaps the border. Inside the paint() function of the delegates, I have used adjust() on the QStyleOptionViewItem's QRect to remove one pixel from the left side and another pixel from the right side of the delegates before I use p->fillRect(rect) to draw the background of each delegate, but that's not an actual solution, because I can't do the same for the top and the bottom (the items would be separated one pixel from one another).

I expected setContentsMargins(1, 1, 1, 1) on the QTreeWidget to do what I want, but it doesn't work.

Any ideas? Thanks