Results 1 to 6 of 6

Thread: Reimplemented QHeaderView paints wrong on scrolling

  1. #1
    Join Date
    May 2016
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Reimplemented QHeaderView paints wrong on scrolling

    Hi. I've reimplemented QHeaderView to rotate text on the vertical header of my table (a QStandardItem through a QSortProxyModel)
    Whenever i scroll to the sides, the painting goes wrong. Like it couldn't paint the new column headers. Also if i do something to fire the paint event, it only paints right if i'm on the initial position. How can i get it to paint properly?
    Thanks in advance for your help.


    Initial position


    Scroll to the furthest right


    Scroll back to initial position



    This is my reimplementation
    Qt Code:
    1. #include <QObject>
    2. #include <QHeaderView>
    3. #include <QPainter>
    4.  
    5. class QHeaderViewR : public QHeaderView
    6. {
    7.  
    8. public:
    9. QHeaderViewR(QStringList _heads):QHeaderView(Qt::Horizontal)
    10. {
    11. heads = _heads;
    12. this->setSectionResizeMode(QHeaderView::Fixed);
    13. this->setSectionsClickable(true);
    14. }
    15.  
    16. void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
    17. {
    18. QPen pen(Qt::black);
    19. painter->setPen(pen);
    20.  
    21. painter->rotate(90);
    22. painter->translate(0,-rect.width()+1);
    23.  
    24. QRect copy = rect;
    25.  
    26. copy.setWidth(rect.height());
    27. copy.setHeight(rect.width());
    28. copy.moveTo(0,-this->sectionPosition(logicalIndex));
    29.  
    30. if (logicalIndex == 0) //Show a line on the left
    31. {
    32. copy.setHeight(rect.width()-1);
    33. painter->drawText(copy,Qt::AlignCenter, heads.at(logicalIndex));
    34. }
    35. else
    36. {
    37. painter->drawText(copy,Qt::AlignCenter ,heads.at(logicalIndex));
    38. }
    39.  
    40. painter->drawRect(copy);
    41. }
    42. signals:
    43. void sectionPressed(int logicalIndex());
    44. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reimplemented QHeaderView paints wrong on scrolling

    What happens if in paintSection you comment out all of your code and call the superclass implementation of the method? Do the problems with repainting go away?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    coyoteazul (31st May 2016)

  4. #3
    Join Date
    May 2016
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reimplemented QHeaderView paints wrong on scrolling

    I didn't know you could do that. Yes, if i do that the repainting is done correctly, but the text is not rotated anymore

    Qt Code:
    1. void paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
    2. {
    3. QHeaderView::paintSection(painter,rect,logicalIndex);
    4. //old code commented out
    5. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reimplemented QHeaderView paints wrong on scrolling

    This works for me just fine:

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. class MyHeader : public QHeaderView {
    4. public:
    5. MyHeader(QWidget*parent = 0) : QHeaderView(Qt::Horizontal, parent) {
    6.  
    7. }
    8. protected:
    9. QSize sectionSizeFromContents(int logicalIndex) const {
    10. QSize s = QHeaderView::sectionSizeFromContents(logicalIndex);
    11. return QSize(s.height(), std::max(100, s.width()));
    12. }
    13.  
    14. void paintSection(QPainter *painter, const QRect & rect, int logicalIndex) const {
    15. if (logicalIndex % 2) {
    16. painter->save();
    17. painter->translate(rect.left(), rect.bottom());
    18. painter->rotate(-90);
    19. QRect r(0, 0, rect.height(), rect.width());
    20. painter->setBrush(Qt::red);
    21. painter->drawRect(r);
    22. painter->drawText(r, Qt::AlignCenter, QString::number(logicalIndex+1));
    23. painter->restore();
    24. } else QHeaderView::paintSection(painter, rect, logicalIndex);
    25. }
    26. };
    27.  
    28. int main(int argc, char**argv) {
    29. QApplication app(argc, argv);
    30. QTableView view;
    31. QStandardItemModel model(800,400);
    32. view.setModel(&model);
    33. view.setHorizontalHeader(new MyHeader);
    34. view.show();
    35. return app.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    coyoteazul (31st May 2016)

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reimplemented QHeaderView paints wrong on scrolling

    Right, I would have also recommended saving and restoring the painter

    Cheers,
    _

  8. #6
    Join Date
    May 2016
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reimplemented QHeaderView paints wrong on scrolling

    It worked!. I tried using save and restoring when i first reimplemented this class, but it wouldn't work as expected for some reason (dumb me probably)
    Thanks a lot!

Similar Threads

  1. Replies: 0
    Last Post: 16th April 2014, 19:04
  2. Replies: 2
    Last Post: 4th April 2012, 10:23
  3. Can't paint in reimplemented QHeaderView
    By Tottish in forum Newbie
    Replies: 5
    Last Post: 21st March 2011, 08:56
  4. QPainter paints only what is seen in QGraphicsView
    By tsd-charlie in forum Qt Programming
    Replies: 1
    Last Post: 8th January 2010, 11:59
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.