Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPaintDevice: Cannot destroy paint device that is being painted?
Forum Updated to NodeBB v4.3 + New Features

QPaintDevice: Cannot destroy paint device that is being painted?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 20.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pauly
    wrote on last edited by
    #1

    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?
    
    K 1 Reply Last reply
    0
    • P Pauly

      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?
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @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

      Vote the answer(s) that helped you to solve your issue(s)

      P 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply
        0
        • K koahnig

          @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

          P Offline
          P Offline
          Pauly
          wrote on last edited by
          #4

          @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?

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

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

            P Offline
            P Offline
            Pauly
            wrote on last edited by
            #5

            @SGaist

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              QPainter is not a QObject.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Pauly
                wrote on last edited by
                #7

                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
                }
                }
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved