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. Resizing mainwindow using resizeEvent works very slow
Forum Updated to NodeBB v4.3 + New Features

Resizing mainwindow using resizeEvent works very slow

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 3 Posters 11.2k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    Hi,
    My Qt widget has a function to draw a line based on point (x, y) input from text file. I want to draw a line even when the window is resized. Everything works fine. I get the right coordinate when the window is maximized or minimized. However, the process is very slow. To maximize or minimize the window, the program will stuck at appx 4sec before it's maximized(minimized) and starts drawing again.

    Are there any way that I can up the speed of the resizeEvent?

    In my Qt widget, I have a function to read from text file, calculate the point (x, y) then write to text file.

    drawpath.cpp

    void DrawPath::drawPath(QString inFile, QString outFile, float width, float height)
    {
        readPosition(inFile);
        calculatedXY(someVector, width, height);
        writePosition(outFile, latLongPointVector);
    }
    

    resizeEvent is called through mainwindow.

    mainwindow.cpp

    void MainWindow::resizeEvent(QResizeEvent *event)
    {
    
        //scaled window size
        scaledWidth = double(event->size().width())/360.0;
        scaledHeight = double(event->size().height())/180.0;
    
        QString inFile = ":/input.txt";
        QString outFile = "output.txt";
    
        //set label window size
        ui->label->setGeometry(0, 0, 360*scaledWidth, 180*scaledHeight);
    
        //set draworbit window size
        path->drawPath(inFile, outFile, scaledWidth, scaledHeight);
        path->setGeometry(0, 0, 360*scaledWidth, 180*scaledHeight);
    }
    

    Thank you.

    Edit: the program will be used for a realtime (x, y) calculation so I need a fast working resize window.

    kshegunovK 1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Why do you use a file to pass data to drawPath()?!
      This is one of the most inefficient ways I can imagine to pass data to a function/method.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      ? 1 Reply Last reply
      0
      • jsulmJ jsulm

        Why do you use a file to pass data to drawPath()?!
        This is one of the most inefficient ways I can imagine to pass data to a function/method.

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @jsulm said:

        Why do you use a file to pass data to drawPath()?!
        This is one of the most inefficient ways I can imagine to pass data to a function/method.

        The raw data I use is from another file type so I need to convert and keep them in text file. However, the text file will be read only one time. I read the data and keep them in a vector, QVector<QPointF>. Then I will use the data from that vector to draw a line.

        SO if this slow resizing is caused by the re-reading file every time the window is resized, are there any way that I fix this?

        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          From documentation:
          "The widget will be erased and receive a paint event immediately after processing the resize event. No drawing need be (or should be) done inside this handler."
          So, you should not draw anything inside resizeEvent()

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          ? 1 Reply Last reply
          1
          • jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            In drawPath() you always call readPosition(inFile);
            I don't know what readPosition() really does, but if it always read the file then you read the file each time you call drawPath().

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            ? 1 Reply Last reply
            0
            • jsulmJ jsulm

              From documentation:
              "The widget will be erased and receive a paint event immediately after processing the resize event. No drawing need be (or should be) done inside this handler."
              So, you should not draw anything inside resizeEvent()

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              @jsulm said:

              So, you should not draw anything inside resizeEvent()

              resizeEvent() does not directly call paintEvent() but I have paintEvent() in my widget to draw a line ....

              When the window is resize, the point (x, y) will be recalculated to match the window size so they get to the right coordinate. If I can not use paintEvent(), how can I draw a line when resize?

              1 Reply Last reply
              0
              • jsulmJ jsulm

                In drawPath() you always call readPosition(inFile);
                I don't know what readPosition() really does, but if it always read the file then you read the file each time you call drawPath().

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                @jsulm

                I have tried to call calculatedXY(someVector, width, height) in resizeEvent() without calling drawPath() but it doesn't seem to work.... it seems like the vector data from readPosition(inFile) doesn't pass to someVector although I have declared it as global variable. Any suggestions?

                1 Reply Last reply
                0
                • jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You should debug your application to find out where it is hanging for 4 seconds.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Well, I don't know how readPosition() is implemented. But in general if the data in the file is static you should read that file only once (for example when your application is starting). To me it looks like you're doing it each time drawPath() is called.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    ? 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      Well, I don't know how readPosition() is implemented. But in general if the data in the file is static you should read that file only once (for example when your application is starting). To me it looks like you're doing it each time drawPath() is called.

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      @jsulm said:

                      Well, I don't know how readPosition() is implemented. But in general if the data in the file is static you should read that file only once (for example when your application is starting). To me it looks like you're doing it each time drawPath() is called.

                      so here is my readPosition()

                      void DrawPath::readPosition(QString inFile_)
                      {
                          //read from text file
                          QFile file(inFile_);
                          if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
                              qDebug() << "error: unable to open...";
                          }
                      
                          QTextStream in(&file);
                      
                          while(!in.atEnd()){
                              QString lineData = in.readLine();
                              QStringList listData = lineData.split("\t");
                      
                              someX << listData[4].toFloat();
                              someY << listData[5].toFloat();
                          }
                      
                          for(int i = 0; i < someX.size()-1; i++){
                      
                              //collect vector data into coordinate(x,y)
                              xyPos.setX(someX[i]);
                              xyPos.setY(someY[i]);
                      
                              //create vector of coordinate(x, y)
                              xyPosVector.append(xyPos);
                          }
                      
                          qDebug() << "Successfully loaded!";
                      
                          file.close();
                      }
                      

                      At the moment, the data is static but it will be updated maybe daily for the complete version of the program. I would like to know if it's gonna be lagged.

                      jsulmJ 1 Reply Last reply
                      0
                      • ? A Former User

                        Hi,
                        My Qt widget has a function to draw a line based on point (x, y) input from text file. I want to draw a line even when the window is resized. Everything works fine. I get the right coordinate when the window is maximized or minimized. However, the process is very slow. To maximize or minimize the window, the program will stuck at appx 4sec before it's maximized(minimized) and starts drawing again.

                        Are there any way that I can up the speed of the resizeEvent?

                        In my Qt widget, I have a function to read from text file, calculate the point (x, y) then write to text file.

                        drawpath.cpp

                        void DrawPath::drawPath(QString inFile, QString outFile, float width, float height)
                        {
                            readPosition(inFile);
                            calculatedXY(someVector, width, height);
                            writePosition(outFile, latLongPointVector);
                        }
                        

                        resizeEvent is called through mainwindow.

                        mainwindow.cpp

                        void MainWindow::resizeEvent(QResizeEvent *event)
                        {
                        
                            //scaled window size
                            scaledWidth = double(event->size().width())/360.0;
                            scaledHeight = double(event->size().height())/180.0;
                        
                            QString inFile = ":/input.txt";
                            QString outFile = "output.txt";
                        
                            //set label window size
                            ui->label->setGeometry(0, 0, 360*scaledWidth, 180*scaledHeight);
                        
                            //set draworbit window size
                            path->drawPath(inFile, outFile, scaledWidth, scaledHeight);
                            path->setGeometry(0, 0, 360*scaledWidth, 180*scaledHeight);
                        }
                        

                        Thank you.

                        Edit: the program will be used for a realtime (x, y) calculation so I need a fast working resize window.

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by kshegunov
                        #11

                        @soloespresso

                        Edit: the program will be used for a realtime (x, y) calculation so I need a fast working resize window.

                        If you want a "realtime" behavior, then thread the IO! It's not the QWidget::resizeEvent's job to read files and do (complicated) calculations. In your worker thread or main thread (by means of scheduling, but it's better to actually thread the operation) do the calculations and update a couple of fields. Then in the resize or paint events do the bare minimum of setting the geometry or painting the data. Events are queued and no event will be processed if the last one hasn't finished, so a slow resize event processing will cause your application to hang, not update widgets and not respond to user input, not process sockets or timers!

                        DrawPath::readPosition is heavy (it reads a whole file), as @jsulm already suspected, and you do that in the event function(s), so everything is waiting for the IO to complete.

                        Kind regards.

                        Read and abide by the Qt Code of Conduct

                        ? 1 Reply Last reply
                        0
                        • ? A Former User

                          @jsulm said:

                          Well, I don't know how readPosition() is implemented. But in general if the data in the file is static you should read that file only once (for example when your application is starting). To me it looks like you're doing it each time drawPath() is called.

                          so here is my readPosition()

                          void DrawPath::readPosition(QString inFile_)
                          {
                              //read from text file
                              QFile file(inFile_);
                              if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
                                  qDebug() << "error: unable to open...";
                              }
                          
                              QTextStream in(&file);
                          
                              while(!in.atEnd()){
                                  QString lineData = in.readLine();
                                  QStringList listData = lineData.split("\t");
                          
                                  someX << listData[4].toFloat();
                                  someY << listData[5].toFloat();
                              }
                          
                              for(int i = 0; i < someX.size()-1; i++){
                          
                                  //collect vector data into coordinate(x,y)
                                  xyPos.setX(someX[i]);
                                  xyPos.setY(someY[i]);
                          
                                  //create vector of coordinate(x, y)
                                  xyPosVector.append(xyPos);
                              }
                          
                              qDebug() << "Successfully loaded!";
                          
                              file.close();
                          }
                          

                          At the moment, the data is static but it will be updated maybe daily for the complete version of the program. I would like to know if it's gonna be lagged.

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

                          @soloespresso readPosition() does not set someVector!
                          If the change frequency of the file is so low then you should not read it all the time - it's just wasted time. You can use QFileSystemWatcher class to detect changes in this file and then reread it.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          ? 1 Reply Last reply
                          0
                          • kshegunovK kshegunov

                            @soloespresso

                            Edit: the program will be used for a realtime (x, y) calculation so I need a fast working resize window.

                            If you want a "realtime" behavior, then thread the IO! It's not the QWidget::resizeEvent's job to read files and do (complicated) calculations. In your worker thread or main thread (by means of scheduling, but it's better to actually thread the operation) do the calculations and update a couple of fields. Then in the resize or paint events do the bare minimum of setting the geometry or painting the data. Events are queued and no event will be processed if the last one hasn't finished, so a slow resize event processing will cause your application to hang, not update widgets and not respond to user input, not process sockets or timers!

                            DrawPath::readPosition is heavy (it reads a whole file), as @jsulm already suspected, and you do that in the event function(s), so everything is waiting for the IO to complete.

                            Kind regards.

                            ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #13

                            @kshegunov said:

                            If you want a "realtime" behavior, then thread the IO!

                            And ow can I do that?

                            I have tried calling only the function to calculate (x, y) without calling drawPath() in resizeEvent() something like this

                            path->calculatedXY(someVector, width, height);
                            

                            but it gives me Runtime Error and "index out of range" for the vector used in that function.

                            kshegunovK 1 Reply Last reply
                            0
                            • ? A Former User

                              @kshegunov said:

                              If you want a "realtime" behavior, then thread the IO!

                              And ow can I do that?

                              I have tried calling only the function to calculate (x, y) without calling drawPath() in resizeEvent() something like this

                              path->calculatedXY(someVector, width, height);
                              

                              but it gives me Runtime Error and "index out of range" for the vector used in that function.

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on last edited by kshegunov
                              #14

                              @soloespresso said:

                              And ow can I do that?

                              Look here for an introduction.

                              but it gives me Runtime Error and "index out of range" for the vector used in that function.

                              See @jsulm's explanation as to why this is happening.
                              Additionally your for loop is wrong:

                              for (int i = 0; i < someX.size()-1; i++)  {
                              

                              this skips the last element in the vector.

                              Kind regards.

                              Read and abide by the Qt Code of Conduct

                              ? 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @soloespresso readPosition() does not set someVector!
                                If the change frequency of the file is so low then you should not read it all the time - it's just wasted time. You can use QFileSystemWatcher class to detect changes in this file and then reread it.

                                ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #15

                                @jsulm

                                @jsulm said:

                                @soloespresso readPosition() does not set someVector!

                                I have some other functions in between. The readPosition() I posted is to give an idea what readPosition() does which is extract the data and keep them in a vector.

                                If the change frequency of the file is so low then you should not read it all the time - it's just wasted time. You can use QFileSystemWatcher class to detect changes in this file and then reread it.

                                OK, I will take a look on that.

                                1 Reply Last reply
                                0
                                • kshegunovK kshegunov

                                  @soloespresso said:

                                  And ow can I do that?

                                  Look here for an introduction.

                                  but it gives me Runtime Error and "index out of range" for the vector used in that function.

                                  See @jsulm's explanation as to why this is happening.
                                  Additionally your for loop is wrong:

                                  for (int i = 0; i < someX.size()-1; i++)  {
                                  

                                  this skips the last element in the vector.

                                  Kind regards.

                                  ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #16

                                  @kshegunov said:

                                  @soloespresso said:

                                  And ow can I do that?

                                  Look here for an introduction.

                                  Ok, I will take a look.

                                  but it gives me Runtime Error and "index out of range" for the vector used in that function.

                                  See @jsulm's explanation as to why this is happening.
                                  Additionally your for loop is wrong:

                                  for (int i = 0; i < someX.size()-1; i++)  {
                                  

                                  this skips the last element in the vector.

                                  Kind regards.

                                  Right!!! I didn't see it. Thank you.

                                  1 Reply Last reply
                                  0
                                  • ? Offline
                                    ? Offline
                                    A Former User
                                    wrote on last edited by
                                    #17
                                    for (int i = 0; i < someX.size()-1; i++)  {
                                    

                                    fix this but still Runtime Error and "index out of range".

                                    I will check QFileSystemWatcher and thread after.

                                    kshegunovK 1 Reply Last reply
                                    0
                                    • ? A Former User
                                      for (int i = 0; i < someX.size()-1; i++)  {
                                      

                                      fix this but still Runtime Error and "index out of range".

                                      I will check QFileSystemWatcher and thread after.

                                      kshegunovK Offline
                                      kshegunovK Offline
                                      kshegunov
                                      Moderators
                                      wrote on last edited by
                                      #18

                                      @soloespresso

                                      fix this but still Runtime Error and "index out of range".

                                      This hasn't anything to do with your error, it was just an observation. As to the assertion (which I'm assuming your code is failing), you should track that in the debugger.

                                      Read and abide by the Qt Code of Conduct

                                      ? 1 Reply Last reply
                                      0
                                      • kshegunovK kshegunov

                                        @soloespresso

                                        fix this but still Runtime Error and "index out of range".

                                        This hasn't anything to do with your error, it was just an observation. As to the assertion (which I'm assuming your code is failing), you should track that in the debugger.

                                        ? Offline
                                        ? Offline
                                        A Former User
                                        wrote on last edited by
                                        #19

                                        @kshegunov said:

                                        @soloespresso

                                        fix this but still Runtime Error and "index out of range".

                                        This hasn't anything to do with your error, it was just an observation. As to the assertion (which I'm assuming your code is failing), you should track that in the debugger.

                                        So let me make sure if what I am doing is ok for Qt.
                                        I have drawPath() in QWidget.

                                        void DrawPath::drawPath(QString inFile, QString outFile, float width, float height)
                                        {
                                            readPosition(inFile);
                                            calculatedXY(someVector, width, height);
                                            writePosition(outFile, latLongPointVector);
                                        }
                                        

                                        Can I call calculatedXY(someVector, width, height); outside drawPath() in resizeEvent() like this?

                                        void MainWindow::resizeEvent(QResizeEvent *event)
                                        {
                                        
                                            //scaled window size
                                            scaledWidth = double(event->size().width())/360.0;
                                            scaledHeight = double(event->size().height())/180.0;
                                        
                                            //QString inFile = ":/input.txt";
                                            //QString outFile = "output.txt";
                                        
                                            //set label window size
                                            ui->label->setGeometry(0, 0, 360*scaledWidth, 180*scaledHeight);
                                        
                                            //set draworbit window size
                                            //path->drawPath(inFile, outFile, scaledWidth, scaledHeight);
                                            path->calculatedXY(someVector, scaledWidth, scaledHeight);
                                            path->setGeometry(0, 0, 360*scaledWidth, 180*scaledHeight);
                                        }
                                        
                                        1 Reply Last reply
                                        0
                                        • kshegunovK Offline
                                          kshegunovK Offline
                                          kshegunov
                                          Moderators
                                          wrote on last edited by
                                          #20

                                          @soloespresso
                                          No one is preventing you from calling calculatedXY(), but if it's a good design choice really depends what that function actually does.

                                          Read and abide by the 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