QPaintDevice: Cannot destroy paint device that is being painted?



  • I'm getting this message at the application output in QtCreator, as the following function exits. This code seems to run ok thought. What is the problem and how can I fix it? Thank you.

    void MyClass::ThisFunc() {
    //some code before this
    QImage image(size, size, QImage::Format_Mono);
    QPainter painter(&image);
    painter.setBrush(background);
    painter.setPen(Qt::NoPen);
    painter.drawRect(0, 0, size, size);
    //and other painter operation
    
    if(image.save(path)){ 
    //do something
    }
    } //message is thrown at this point. I guess image can not be destroy? why?

  • Moderators

    @Pauly

    Some things you cannot destroy while you are working on those a slot function. My guess is that there is your problem.

    void MyClass::ThisFunc() {
    //some code before this
    QImage image(size, size, QImage::Format_Mono);
    QPainter *painter = new QPainter (&image) ;
    painter->setBrush(background);
    painter->setPen(Qt::NoPen);
    painter->drawRect(0, 0, size, size);
    //and other painter operation
    
    if(image.save(path)){ 
    //do something
    }
    painter->deleteLater();   // will delete the paint device outside of this slot by event loop
    } //message is thrown at this point. I guess image can not be destroy? why?
    

    See, if it helps


  • Lifetime Qt Champion

    Hi,

    Aren't you missing a call to QPainter::end ?



  • @koahnig said in QPaintDevice: Cannot destroy paint device that is being painted?:

    painter->deleteLater();

    It said 2165: error: 'class QPainter' has no member named 'deleteLater' painter->deleteLater();

    am I missing something?



  • @SGaist

    I tried added painter.end();, but the problem persists.


  • Lifetime Qt Champion

    QPainter is not a QObject.



  • Thank you all for the help.

    Following the lead, I think the problem is that QImage "image" can not be destroyed as QPainterDevice. So I double check the code and I found I assign something to image along the way, I think that causes the problem. I assign it to new QImage and leave along the original QImage image, the error seems to go way. Do you think this makes sense? Thank you.

    void MyClass::ThisFunc() {
    //some code before this
    QImage image(size, size, QImage::Format_Mono);
    QPainter painter(&image);
    painter.setBrush(background);
    painter.setPen(Qt::NoPen);
    painter.drawRect(0, 0, size, size);
    //and other painter operation
    
    **image=something_else**
    
    if(image.save(path)){ 
    //do something
    }
    }

  • Lifetime Qt Champion

    Do you mean you assigned a new value to your image variable before calling painter.end() ?


Log in to reply
 

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