Results 1 to 6 of 6

Thread: Can't paint in reimplemented QHeaderView

  1. #1
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Can't paint in reimplemented QHeaderView

    Hi!
    I'm having a subclass of QTableView and want to draw icons, the size of which I can freely adjust, in the Horizontal Header and also for the header to support stylesheets.

    From googling around for a while I've been lead to understand that it is not possible without reimplementing the QHeaderView since this class does not support delegates.

    My plan was/is to inherit QHeaderView into a new class (IconHeaderView) and Override the paintSection method in which I would first call QHeaderView::paintSection for it to paint exactly as it does now (with no visible text data in the "header-buttons") and after that, paint my icon on top of the button.
    But I cant get it to work. Nothing visible seems to get drawn on top of the buttons (which render just fine, with or without stylesheets set) using this code:

    Qt Code:
    1. void IconHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
    2. {
    3.  
    4. QHeaderView::paintSection(painter, rect, logicalIndex);
    5.  
    6.  
    7. QPixmap myPixMap(":images/icons/header/ERSicoBUSS.png");
    8. QRect drawingRect;
    9. drawingRect.setHeight(22);
    10. drawingRect.setWidth(60);
    11. drawingRect.moveCenter(rect.center());
    12.  
    13. painter->drawPixmap(drawingRect, myPixMap);
    14.  
    15. qDebug() << "done drawing pixmap";
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    Can anyone tell me what is wrong? I'm guessing there is something funny about the painter used since the exact same code works like a charm in the delegate used for the rest of the table-view.
    Of course I am also open to different approaches as long as it satisfies my requirements: freely resizeble icons and stylesheet support.

    Thanks a bunch!
    /Tottish

    EDIT: Hmm, it seems to work fine if I create a new QPainter on the viewport as so: QPainter myPainter(viewport()). Kind of solves my problem but could someone tell me why is this?
    Last edited by Tottish; 26th February 2011 at 19:11.

  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: Can't paint in reimplemented QHeaderView

    Try this:
    Qt Code:
    1. void IconHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
    2. {
    3. painter->save();
    4. QHeaderView::paintSection(painter, rect, logicalIndex);
    5. painter->restore();
    6.  
    7. QPixmap myPixMap(":images/icons/header/ERSicoBUSS.png");
    8. QRect drawingRect;
    9. drawingRect.setHeight(22);
    10. drawingRect.setWidth(60);
    11. drawingRect.moveCenter(rect.center());
    12.  
    13. painter->drawPixmap(drawingRect, myPixMap);
    14.  
    15. qDebug() << "done drawing pixmap";
    16.  
    17. }
    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.


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

    deepal_de (17th March 2011)

  4. #3
    Join Date
    Mar 2010
    Posts
    77
    Thanks
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't paint in reimplemented QHeaderView

    Well that certainly did the trick!
    So the painter object is being modified inside the QHeaderView:aintSection-event?

    As always, my hat goes off to you wysota! Thanks!
    /Tottish

  5. #4
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Question Re: Can't paint in reimplemented QHeaderView

    i need to add vertical text to my Qtreeview header...
    can u help me,
    im new to QT,still learning

    this is my code

    Qt Code:
    1. #ifndef MYHEADERVIEW_H
    2. #define MYHEADERVIEW_H
    3.  
    4. #include <QWidget>
    5. #include <QHeaderView>
    6. #include <QPainter>
    7.  
    8. class myheaderView : public QHeaderView
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. myheaderView(QWidget *parent = 0);
    14. ~myheaderView();
    15.  
    16. void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
    17. {
    18. painter->save();
    19. QHeaderView::paintSection(painter, rect, logicalIndex);
    20. painter->restore();
    21.  
    22. painter->setPen(Qt::black);
    23. painter->setBrush(Qt::black);
    24. painter->rotate(90);
    25. painter->drawText(10,10,"TEXT");
    26.  
    27. }
    28. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QTreeView *p = new QTreeView(this);
    2. myheaderView *pH = new myheaderView(p);
    3. p->setProperty("pos",QPoint(10,10));
    To copy to clipboard, switch view to plain text mode 

    can you point me in the right direction ?
    Last edited by wysota; 17th March 2011 at 08:37. Reason: missing [code] tags

  6. #5
    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: Can't paint in reimplemented QHeaderView

    You can't just rotate the painter. You have to recalculate the coordinates as the painter rotates around its (0,0) point and not around the origin of the header section. You should translate the painter properly before drawing and then back after drawing.
    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.


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

    deepal_de (20th March 2011)

  8. #6
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Question Re: Can't paint in reimplemented QHeaderView

    Hello,
    I managed to get the rotated text,but its not giving the expected output
    Need to draw the text in all the columns in header.
    the text disperse sometime... Can you help me to correct this,

    void CustomHeader:aintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
    {
    painter->save();
    QHeaderView:aintSection(painter, rect, logicalIndex);
    painter->restore();

    painter->save();
    painter->translate(20,20/*rect.bottomLeft().x()+20,75*/);
    painter->rotate(270);
    painter->drawText(0,-(rect.width()*logicalIndex), "Text");
    painter->restore();
    }

    thanks you..
    Last edited by deepal_de; 21st March 2011 at 09:01.

Similar Threads

  1. QHeaderView
    By waynew in forum Qt Programming
    Replies: 2
    Last Post: 25th December 2009, 20:42
  2. Replies: 2
    Last Post: 4th December 2009, 06:15
  3. QGraphicsItem reimplemented mouse events
    By aarelovich in forum Qt Programming
    Replies: 12
    Last Post: 24th July 2009, 12:56
  4. QHeaderView
    By gyre in forum Qt Tools
    Replies: 1
    Last Post: 25th September 2007, 16:35
  5. QHeaderView as a banner
    By mclark in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2007, 19:28

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.