Results 1 to 3 of 3

Thread: Problem with QListWidget and paintEvent()

  1. #1
    Join Date
    May 2009
    Location
    Tampere Finland
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Problem with QListWidget and paintEvent()

    I have inherited QListWidget and rewrote the paintEvent()-function as follows:

    Qt Code:
    1. void InheritedListWidget::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this);
    4. painter.setRenderHint(QPainter::Antialiasing, true);
    5.  
    6. static const QPointF points[3] = {
    7. QPointF(2,2),
    8. QPointF(this->width()-2,2),
    9. QPointF(2,this->height()-2)
    10. };
    11. painter.setBrush(Qt::black);
    12. painter.drawPolygon(points, 3);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Now, for some reason when I'm running the program, nothing is painted on the list widget and the application prints to console:

    Qt Code:
    1. QPainter::begin: Paint device returned engine == 0, type: 1
    2. QPainter::setRenderHint: Painter must be active to set rendering hints
    3. QPainter::setBrush: Painter not active
    To copy to clipboard, switch view to plain text mode 

    What is wrong with my code?

  2. #2
    Join Date
    May 2009
    Posts
    1
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QListWidget and paintEvent()

    You should give viewport to QPainter.

    Qt Code:
    1. QPainter painter(this->viewport());
    To copy to clipboard, switch view to plain text mode 

    See Qt docs:

    void QAbstractScrollArea:: paintEvent ( QPaintEvent * event ) [virtual protected]

    This event handler can be reimplemented in a subclass to receive paint events (passed in event), for the viewport() widget.

    Note: If you open a painter, make sure to open it on the viewport().

  3. The following 3 users say thank you to birchbay for this useful post:

    canislupax (14th November 2010), joksi (29th May 2009), San_Cou (6th February 2013)

  4. #3
    Join Date
    May 2009
    Location
    Tampere Finland
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Problem with QListWidget and paintEvent()

    Thanks birchbay, this solved my problem!

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.