[SOLVED] 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::paintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::setPen: Painter not active

    How can I do that?

    @
    #include "fluxocaixawidget.h"

    fluxoCaixaWidget::fluxoCaixaWidget(QWidget *parent) :
    QWidget(parent)
    {

    }

    void fluxoCaixaWidget::paintEvent(QPaintEvent *)
    {
    QPainter painter(this);

    QPen caneta(Qt::black);
    caneta.setWidth(4);
    painter.setPen(caneta);
    
    
    
    painter.drawLine(20, this->parentWidget()->height() / 2 - 20, this->parentWidget()->width()-40, this->parentWidget()->height() / 2 - 20);
    

    }

    void fluxoCaixaWidget::desenhaFluxo()
    {
    QPainter painter(this);

    QPen caneta(Qt::black);
    caneta.setWidth(4);
    painter.setPen(caneta);
    
    painter.drawLine(10,10,50,10);
    

    }

    @


  • Moderators

    Quoting from the documentation:
    [quote]Each widget performs all painting operations from within its paintEvent() function. This is called whenever the widget needs to be redrawn, either as a result of some external change or when requested by the application.[/quote]

    I hope the reason for your warnings is clear now.



  • Thanks for the reply Sierdzio!

    I got it. But how could I do draw other lines calling a function like the code above?



  • Re-implement paintEvent() and perform all required drawing operations in there. As the docs say, Qt will call that method whenever your Widget needs to be (re)drawn. Of course you can call sub-functions, like desenhaFluxo(), from there. Just do not call functions which perform painting operations directly!



  • Worked fine after clean the project


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.