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. How to pause and resume a progress bar in Qt 4.7

How to pause and resume a progress bar in Qt 4.7

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 4.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.
  • P Offline
    P Offline
    phamvanan
    wrote on last edited by
    #1

    Hi all,
    I want create a progress bar for copy files progressing from folder to another folder. But i don't how to pause and resume copy progressing, same time status of progress bar pause or resume also.
    Thank you for any your help.

    -PVA-

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      you set the progress of the progressbar with setValue() right? if your copy process pauses it implicitly also pauses the progressbar then ... or do i misunderstand something here?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phamvanan
        wrote on last edited by
        #3

        Thanks for your response soon,
        my snippet code:
        @for(int i = 1; i <= totalFiles; i++){
        ui->progressBar->setValue(i);
        QString file = qsl.at(i-1);
        QFileInfo finfo(QString("%1/%2").arg(src->path()).arg(file));
        ui->lbl_CurrentFont->setText(finfo.fileName());
        QFile::copy(finfo.filePath(),des->path()+ "/" + finfo.fileName());
        ui->lbl_FontRemaining->setText(QString("%1 fonts remaining").arg((totalFiles - i)));
        }@

        I don't know how to pause for loop and resume also.
        What do us need to catch user click pause button. e.g: TimerEvent...
        Thank you again.
        An.

        -PVA-

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          well... this is way more complicated then. You can only achieve what you want with threads and a busy loop.

          A nicer solution would be to hold 2 QFile objects and read/write chunks.

          Here a very minimalistic example (without error handling) to give you an idea:
          @
          MyCopyClass::MyCopyClass()
          {
          m_ReaderFile.setFileName("C:/read.txt");
          m_ReaderFile.open(QIODevice::ReadOnly);

          m_WriterFile.setFileName("C:/write.txt");
          m_WriterFile.open(QIODevice::WriteOnly);
          

          }

          void MyCopyClass::pause() //SLOT
          {
          m_CopyPaused = true;
          }

          void MyCopyClass::resume() //SLOT
          {
          if( m_CopyPaused )
          {
          m_CopyPaused = false;
          this->copy();
          }
          }

          void MyCopyClass::copy() //SLOT
          {
          if( m_CopyPaused )
          return;

          QByteArray chunk = m_ReaderFile.read(1024); //QFile
          m_WriterFile.write(chunk, 1024);
          
          //set progress of progressbar
          
          QTimer::singleShot(0, this, SLOT(copy()));
          

          }
          @

          Nevertheless i would execute every copy (this class) in an own thread.
          You can connect your buttons to the pause/resume slots but ensure that you use a Qt::QueuedConnection when using threads.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • P Offline
            P Offline
            phamvanan
            wrote on last edited by
            #5

            Hi, your example is copy content file to new file but my situation is copy all files in a folder, i think it same together. by the way, please tell me resolve if user's to destroy copy progressing, all file copied before will be rollback. how to do that?
            as i know, we have a QDir.reomove(filename) function to do that, true or not?
            I try to use it but not working.
            Thanks a lot.
            An.

            -PVA-

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vezprog
              wrote on last edited by
              #6

              I think a thread would be a good choice in this situation. Signals and slots are your best friend.

              As the thread copy is happening you can fire a signal in your loop that updates the progress bar in your main GUI telling it where it should be. You could even implement are "start" and "finished" signal, so when the copy begins, "start" should show your progress bar and initialize it to a specific value, get updates in-between while the copy is happening, then the "finished" signal would show some text say it has completed a 100%.

              Or just one signal would suffice with 2 arguments, a command, and a value.

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

                Hi, I understand you say but I don't where begin implement?
                sorry you, I'm beginner coding Qt.
                Can you give your snippet coding example, please?
                Thank you and best regards.
                An.

                -PVA-

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  [quote author="phamvanan" date="1370622205"]Hi, your example is copy content file to new file but my situation is copy all files in a folder, i think it same together. by the way, please tell me resolve if user's to destroy copy progressing, all file copied before will be rollback. how to do that?
                  as i know, we have a QDir.reomove(filename) function to do that, true or not?
                  [/quote]

                  the concept will be the same. You will get one folder for input and one folder for output. Now you can read all the files with "QDir::entryList()":http://qt-project.org/doc/qt-4.8/qdir.html#entryList-2 and do the copying like i exampled.
                  For undoing the files already copied you just need to store a list of the files you have already written and just delete them when you abort the copying before it's finished.

                  [quote author="phamvanan" date="1370622205"]I try to use it but not working.[/quote]
                  What exactly is not working?!
                  You need to specify a file as parameter not a folder. So you will need to the recursion within a loop.

                  You haven't chosen the right application to start programming in my opinion since it has some complexity in it and it needs some class design skills. But you leanr the most out of errors made ;)

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  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