Results 1 to 4 of 4

Thread: QHeaderView paintSection reimplementation

  1. #1
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QHeaderView paintSection reimplementation

    I reimplemented QHeaderView so I could make it look more like my QTreeView.

    I have got this code:
    Qt Code:
    1. MusicListHeaderView::MusicListHeaderView(QWidget* parent): QHeaderView(Qt::Horizontal, parent)
    2. {
    3. hoverIndex = -1;
    4. }
    5.  
    6. void MusicListHeaderView::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
    7. {
    8. drawBackground(painter, rect, logicalIndex);
    9. drawText(painter, rect, logicalIndex);
    10. }
    11.  
    12. bool MusicListHeaderView::event(QEvent* event)
    13. {
    14. switch(event->type())
    15. {
    16. case QEvent::HoverEnter:
    17. {
    18. QHoverEvent* hover = static_cast<QHoverEvent*>(event);
    19. int hoverIndex = logicalIndexAt(hover->pos());
    20. }
    21. break;
    22. case QEvent::HoverLeave:
    23. {
    24. hoverIndex = -1;
    25. }
    26. break;
    27. case QEvent::HoverMove:
    28. {
    29. QHoverEvent* hover = static_cast<QHoverEvent*>(event);
    30. hoverIndex = logicalIndexAt(hover->pos());
    31. }
    32. break;
    33. }
    34. return QHeaderView::event(event);
    35. }
    36.  
    37. void MusicListHeaderView::drawBackground(QPainter* painter, const QRect& rect, int index) const
    38. {
    39. if(hoverIndex == index)
    40. painter->fillRect(rect, QColor(0xF0, 0xF0, 0xF0));
    41. else
    42. painter->fillRect(rect, Qt::white);
    43. painter->setPen(QColor(0xF0, 0xF0, 0xF0));
    44. painter->drawLine(rect.x(), rect.y()+rect.height()-1, rect.x()+rect.width(), rect.y()+rect.height()-1); // horizontal
    45. // painter->drawLine(rect.x()+rect.width()-1, rect.y(), rect.x()+rect.width()-1, rect.y()+rect.height()); // vertical
    46. }
    47.  
    48. void MusicListHeaderView::drawText(QPainter* painter, const QRect& rect, int index) const
    49. {
    50. QFontMetrics metric(painter->font());
    51. QString final = "test";
    52.  
    53. painter->setPen(Qt::black);
    54. painter->setFont(QFont("Corbel", 9, QFont::Bold));
    55. painter->drawText(rect.adjusted(10, 0, 10, 0), Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, final);
    56. }
    To copy to clipboard, switch view to plain text mode 

    But now when I resize my column to the size where it gets smaller than the view, then the standard header is drawn (check attachment: upper right corner).

    How can I fix this? Should I reimplement the whole paintevent?

    Thanks in advance,
    Gillis
    Attached Images Attached Images
    Last edited by supergillis; 24th November 2008 at 17:31.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHeaderView paintSection reimplementation

    What are u setting the header view on ? is it a tableview ??
    There can be some simpler way to do it...

  3. #3
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QHeaderView paintSection reimplementation

    Yes there was indeed a simpler way to do it . I made a custom style and reimplemented drawControl

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QHeaderView paintSection reimplementation

    I guess it can be even simpler ,
    from ur code i can make out the following - u need to subclass headerview so that u can underline it....
    also u dont want that when u resize the column, a extra column shud appear.

    May be you should look at QHeaderView::setStretchLastSectionbec as far as i understand, u want the tableview to behave as treeview... i had done somethng similar, so if u can explain, may be we can help

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.