Results 1 to 3 of 3

Thread: Paint a border around a QTreeWidget

  1. #1
    Join Date
    Sep 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Paint a border around a QTreeWidget

    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

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Paint a border around a QTreeWidget

    Hello,

    Have you tried setting QTreeWidget FrameShape and FrameShadow?
    QTreeWidget is derived from QFrame, so you can use
    Qt Code:
    1. ui->myTreeWidget->setFrameShape(QFrame::Box);
    2. ui->myTreeWidget->setFrameShadow(QFrame::Plain);
    To copy to clipboard, switch view to plain text mode 
    to set the frame around your widget. Using these two functions (or specifying the corresponding attributes in designer) there is no need for overriding the paint event in your derived TreeWidget class.

    Best regards
    ars

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

    m0nq1 (8th January 2016)

  4. #3
    Join Date
    Sep 2015
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Paint a border around a QTreeWidget

    Thank you very much ars. I had tried this, or so I thought, but I didn't like the result. Now I tried it again and it's what I was looking for. Well, what can I say.

    Greetings.

Similar Threads

  1. Replies: 0
    Last Post: 21st October 2013, 10:16
  2. Replies: 10
    Last Post: 9th September 2013, 16:29
  3. Replies: 1
    Last Post: 3rd January 2013, 14:11
  4. Replies: 2
    Last Post: 21st March 2012, 15:30
  5. paint Rect sometimes missing one border
    By stella1016 in forum Qt Programming
    Replies: 5
    Last Post: 20th January 2011, 20:39

Tags for this Thread

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.