Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt app freeze when remove usb drive while qpainter is drwaing

Qt app freeze when remove usb drive while qpainter is drwaing

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
qpainter
11 Posts 3 Posters 1.3k Views
  • 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.
  • V Offline
    V Offline
    Vijay R. Gawade
    wrote on last edited by
    #1

    painter = new QPainter();
    printer = new QPrinter();
    printer->setOutputFormat(QPrinter::PdfFormat);
    printer->setFullPage(true);
    printer->setResolution(60);
    printer->setOrientation(QPrinter::Portrait);
    printer->setPaperSize(QPrinter::A3);
    // /media/sda1/EventLog.pdf
    printer->setOutputFileName(destLog+filename+".pdf");
    painter->begin(printer);

    Note: painter & printer are class member variable

    On usb device remove signal we try to call following at which app freeze
    painter->end();

    If we skip above end() call and try to print next time following error thrown:
    11.26 06:42:28.681 WRN DEV: :0 --> QPrinter::setOutputFileName: Cannot be changed while printer is active
    11.26 06:42:28.682 WRN DEV: :0 --> QPainter::begin: A paint device can only be painted by one painter at a time.

    Can you suggest how we can handle usb device removed while painting is in progress?

    qt 5.9.4 is used

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

      Hi and welcome to devnet,

      Are you properly deleting your painter and printer objects ? From the looks of it you are currently leaking them.

      These class are usually instanciated on the stack and not the heap.

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

      V 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Are you properly deleting your painter and printer objects ? From the looks of it you are currently leaking them.

        These class are usually instanciated on the stack and not the heap.

        V Offline
        V Offline
        Vijay R. Gawade
        wrote on last edited by
        #3

        @SGaist How we can delete qprinter object? I have called painter->end() call usb stick is removed but after looking logs when we call painter->end() methods process freeze.
        PS O/P:
        root 1133 3.0 0.0 0 0 ? Zl 22:24 0:08 [MyApp] <defunct>

        Since we pass QPrinter as device to Qpainter and QPrinter output file path is set on usb device location which will be unavailable after removing usb stick, may be that's why paint->end() call failed becoz of resource unavailable

        jsulmJ 1 Reply Last reply
        0
        • V Vijay R. Gawade

          @SGaist How we can delete qprinter object? I have called painter->end() call usb stick is removed but after looking logs when we call painter->end() methods process freeze.
          PS O/P:
          root 1133 3.0 0.0 0 0 ? Zl 22:24 0:08 [MyApp] <defunct>

          Since we pass QPrinter as device to Qpainter and QPrinter output file path is set on usb device location which will be unavailable after removing usb stick, may be that's why paint->end() call failed becoz of resource unavailable

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @Vijay-R-Gawade said in Qt app freeze when remove usb drive while qpainter is drwaing:

          How we can delete qprinter object?

          Like any other object allocated via new:

          delete printer;
          

          But as @SGaist pointed out there is no need to allocated on the heap via new, just allocate on the stack:

          QPainter painter;
          
          V 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Vijay-R-Gawade said in Qt app freeze when remove usb drive while qpainter is drwaing:

            How we can delete qprinter object?

            Like any other object allocated via new:

            delete printer;
            

            But as @SGaist pointed out there is no need to allocated on the heap via new, just allocate on the stack:

            QPainter painter;
            
            V Offline
            V Offline
            Vijay R. Gawade
            wrote on last edited by
            #5

            @jsulm I have tried to delete printer & painter with delete call and reinitialize both with new keyword, but app freezes as previous.
            root 1149 14.0 0.0 0 0 ? Zl 00:46 0:11 [MyApp] <defunct>

            jsulmJ 1 Reply Last reply
            0
            • V Vijay R. Gawade

              @jsulm I have tried to delete printer & painter with delete call and reinitialize both with new keyword, but app freezes as previous.
              root 1149 14.0 0.0 0 0 ? Zl 00:46 0:11 [MyApp] <defunct>

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Vijay-R-Gawade Why don't you simply allocate them on the stack? Stack allocation is way faster than heap and you do not have to bother to delete the stuff again.

              Can you show your current code?

              V 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Vijay-R-Gawade Why don't you simply allocate them on the stack? Stack allocation is way faster than heap and you do not have to bother to delete the stuff again.

                Can you show your current code?

                V Offline
                V Offline
                Vijay R. Gawade
                wrote on last edited by
                #7

                @jsulm

                ReportGenerator::ReportGenerator(QObject *parent) : QObject(parent)
                {
                    pdfTimer = new QTimer();
                    pdfTimer->setInterval(200);
                    connect(pdfTimer, SIGNAL(timeout()), this, SLOT(startPDFTimer()));
                
                    painter = new QPainter();
                
                    printer = new QPrinter();
                    printer->setOutputFormat(QPrinter::PdfFormat);
                    printer->setFullPage(true);
                    printer->setResolution(60);
                    printer->setOrientation(QPrinter::Portrait);
                    printer->setPaperSize(QPrinter::A3);
                }
                
                void ReportGenerator::pdfReport()
                {
                // reinitialize painter & printer  same as in constr when deleting obj in case of usb remove in startPDFTimer() func
                    printer->setOutputFileName(filename + ".pdf"); //media/sda1/EventLog.pdf
                
                    painter->begin(printer);
                
                    // drwaing some header using painter
                
                    pdfTimer->Start()
                }
                
                void ReportGenerator::startPDFTimer()
                {
                    // Read records from DB
                    // And draw using painter
                
                // Note: rm->usbIsReady: have state usb device connected or not
                    if (!globalQuery.next())
                    {
                        pdfTimer->stop();
                        painter->end();
                
                        emit reportGenerated();
                    }
                    else
                    {
                        // USB removed while some more records remaining
                        if (!rm->usbIsReady())
                        {
                            pdfTimer->stop();
                //delete painter;
                        //delete printer;
                            painter->end();
                
                            emit showErrorPopup()
                        }
                    }
                }
                
                jsulmJ 1 Reply Last reply
                0
                • V Vijay R. Gawade

                  @jsulm

                  ReportGenerator::ReportGenerator(QObject *parent) : QObject(parent)
                  {
                      pdfTimer = new QTimer();
                      pdfTimer->setInterval(200);
                      connect(pdfTimer, SIGNAL(timeout()), this, SLOT(startPDFTimer()));
                  
                      painter = new QPainter();
                  
                      printer = new QPrinter();
                      printer->setOutputFormat(QPrinter::PdfFormat);
                      printer->setFullPage(true);
                      printer->setResolution(60);
                      printer->setOrientation(QPrinter::Portrait);
                      printer->setPaperSize(QPrinter::A3);
                  }
                  
                  void ReportGenerator::pdfReport()
                  {
                  // reinitialize painter & printer  same as in constr when deleting obj in case of usb remove in startPDFTimer() func
                      printer->setOutputFileName(filename + ".pdf"); //media/sda1/EventLog.pdf
                  
                      painter->begin(printer);
                  
                      // drwaing some header using painter
                  
                      pdfTimer->Start()
                  }
                  
                  void ReportGenerator::startPDFTimer()
                  {
                      // Read records from DB
                      // And draw using painter
                  
                  // Note: rm->usbIsReady: have state usb device connected or not
                      if (!globalQuery.next())
                      {
                          pdfTimer->stop();
                          painter->end();
                  
                          emit reportGenerated();
                      }
                      else
                      {
                          // USB removed while some more records remaining
                          if (!rm->usbIsReady())
                          {
                              pdfTimer->stop();
                  //delete painter;
                          //delete printer;
                              painter->end();
                  
                              emit showErrorPopup()
                          }
                      }
                  }
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    What is the logic behind using that timer ?

                    You might also want to consider QPdfWriter.

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

                    V 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      What is the logic behind using that timer ?

                      You might also want to consider QPdfWriter.

                      V Offline
                      V Offline
                      Vijay R. Gawade
                      wrote on last edited by
                      #10

                      @SGaist Timer is used to add some delay to Update Progress bar on UI by one step when each record from DB is drawn.
                      Right now I'm sure problem with paint->end() library call when usb stick is removed.
                      So I changed the O/P file path to Device temp dir and when all done copy it to usb device. And in case of usb-stick is removed at middle, paint->end() will call as normal (not crashing since Device Temp path available always)

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

                        @Vijay-R-Gawade said in Qt app freeze when remove usb drive while qpainter is drwaing:

                        @SGaist Timer is used to add some delay to Update Progress bar on UI by one step when each record from DB is drawn.

                        That's a fishy procedure to update your progress bar.

                        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
                        1

                        • Login

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