Results 1 to 2 of 2

Thread: Subclassing QWidget not working

  1. #1
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Subclassing QWidget not working

    Hello all!

    I need to subclass QWidget, but its not working. When I call the function desenhaFluxo(), it returns the error:
    QWidget:aintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::setPen: Painter not active

    How can I do that?

    Qt Code:
    1. #include "fluxocaixawidget.h"
    2.  
    3. fluxoCaixaWidget::fluxoCaixaWidget(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6.  
    7. }
    8.  
    9. void fluxoCaixaWidget::paintEvent(QPaintEvent *)
    10. {
    11. QPainter painter(this);
    12.  
    13. QPen caneta(Qt::black);
    14. caneta.setWidth(4);
    15. painter.setPen(caneta);
    16.  
    17.  
    18.  
    19. painter.drawLine(20, this->parentWidget()->height() / 2 - 20, this->parentWidget()->width()-40, this->parentWidget()->height() / 2 - 20);
    20. }
    21.  
    22. void fluxoCaixaWidget::desenhaFluxo()
    23. {
    24. QPainter painter(this);
    25.  
    26. QPen caneta(Qt::black);
    27. caneta.setWidth(4);
    28. painter.setPen(caneta);
    29.  
    30. painter.drawLine(10,10,50,10);
    31. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Subclassing QWidget not working

    You cannot paint on a widget outside its paintEvent() method,

    Either store the line parameters in your class and use the in paintEvent() and trigger an update by calling update() in desenhaFluxo()
    or
    Paint into a QPixmap and let paintEvent() only paint that pixmap.

    Cheers,
    _

Similar Threads

  1. Subclassing a QWidget
    By aguleo in forum Newbie
    Replies: 18
    Last Post: 23rd February 2013, 16:19
  2. Replies: 0
    Last Post: 28th October 2011, 18:46
  3. Subclassing QWidget and QPushButton
    By ber0y in forum Newbie
    Replies: 7
    Last Post: 24th July 2009, 10:25
  4. subclassing QWidget
    By BeS in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2009, 20:38
  5. Replies: 27
    Last Post: 13th September 2007, 09:01

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.