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. Using two QProgressBar in MainWindow
QtWS25 Last Chance

Using two QProgressBar in MainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
progressbar
11 Posts 4 Posters 2.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.
  • K Offline
    K Offline
    koahnig
    wrote on last edited by
    #1

    I have two QPrgressBars on my MainWindow widget. They are getting updated only in the beginning.

    Basically I am using two loops for copying files. One is defining the number of jobs while the other is defining the number of files in each Job. As already written in the beginning it seem to work up to 25% percent of Jobs and suddenly there are not updates anymore.

    Before I had used QProgressDialog with Jobs. While I was updating the progress dialog and one progress bar all (files bar was not implemented yet) where performing allright. After removing the separate progress dialog the problem started.

    Basically the whole copying is done in one slot of the main window. Would I need to shift that into a separate thread?
    Is not really logical because that worked and suddenly stopped.

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

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

      Hi,

      How are you implementing the file copy ?

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

      K 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        How are you implementing the file copy ?

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        @SGaist

        Basically a loop over QFile::copy. It is clear that the bytes are not countable, but that I am not trying. It is simply counting the files copied.

        There is one loop of jobs (each job has a different target location) and in the jobs a number of files. One progress bar shows the progress with respect to jobs and the other shows the progress of files in each job and goes for each job from 0 to max value.

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

        JonBJ 1 Reply Last reply
        0
        • K koahnig

          @SGaist

          Basically a loop over QFile::copy. It is clear that the bytes are not countable, but that I am not trying. It is simply counting the files copied.

          There is one loop of jobs (each job has a different target location) and in the jobs a number of files. One progress bar shows the progress with respect to jobs and the other shows the progress of files in each job and goes for each job from 0 to max value.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @koahnig
          You would need to show code.

          K 1 Reply Last reply
          0
          • JonBJ JonB

            @koahnig
            You would need to show code.

            K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            @JonB
            The class definition

            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            #include <QProgressDialog>
            
            namespace Ui {
            class MainWindow;
            }
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
                QProgressDialog *Progress;
            
            public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();
            
            public slots:
                void splitCopyJobs ();
                void on_pbSource_pressed();
                void on_pbTarget_pressed();
            
                void sltJobs ( int cnt, int maxCnt );
                void sltFiles ( int cnt, int maxCnt );
            private:
                Ui::MainWindow *ui;
            };
            
            #endif // MAINWINDOW_H
            
            

            The implementation file

            #include "MainWindow.h"
            #include "ui_MainWindow.h"
            
            #include <QDebug>
            #include <QFileDialog>
            #include <QTimer>
            
            #include "SplitCopyJobs.h"
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                Progress = 0;
                ui->pbJobs->setVisible( false );
                ui->pbFiles->setVisible( false );
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::splitCopyJobs()
            {
                if ( QDir ( ui->pbSource->text() ).exists() && QDir( ui->pbTarget->text() ).exists() )
                {
                    SplitCopyJobs jobs;
                    connect( &jobs, &SplitCopyJobs::sigJobs, this, &MainWindow::sltJobs );
                    connect( &jobs, &SplitCopyJobs::sigCopying, this, & MainWindow::sltFiles );
                    jobs.runSplitCopy ( ui->pbSource->text(), ui->pbTarget->text() );
                    ui->pbSource->setText( "-- Select --" );
                    ui->pbTarget->setText( "-- Select --" );
                }
            }
            
            void MainWindow::sltJobs ( int cnt, int maxCnt )
            {
            //    if ( !Progress )
            //    {
            //        Progress = new QProgressDialog ( "Number of jobs ", QString (), 0, maxCnt );
            //        Progress->setWindowModality( Qt::WindowModal );
            //    }
            //    Progress->setValue ( cnt );
            
                if ( ! cnt )
                {
                    ui->pbJobs->setMinimum ( 0 );
                    ui->pbJobs->setMaximum ( maxCnt );
                    ui->pbJobs->setVisible ( true );
                    ui->pbJobs->setWindowModality( Qt::WindowModal );
                }
                ui->pbJobs->setValue ( cnt );
            }
            
            void MainWindow::sltFiles ( int cnt, int maxCnt )
            {
                if ( ! cnt )
                {
                    ui->pbFiles->setMinimum ( 0 );
                    ui->pbFiles->setMaximum ( maxCnt );
                    ui->pbFiles->setVisible ( true );
                    ui->pbFiles->setWindowModality( Qt::WindowModal );
                }
                ui->pbFiles->setValue ( cnt );
            }
            

            Screenshot of ui
            0_1520352485174_d06c64e2-b633-4dea-b8b3-6355435d8afd-image.png

            Basically all is done in splitCopyJobs and i is also blocking the event loop the whole time until all files are copied.

            However, when this is the problem, why would then the progress bars change at all?

            As the code had some separate progress dialog is shown above. The progress dialog and at the same time progress bars were showing all as expected. After commenting out the progress dialog all seem to work, but only until the upper bar reached 25%. In the background the files were still copied, but both bars remained frozen.

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

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

              Might be a silly question but are you sure that the counter and maximum values are accurate ?

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

              K 1 Reply Last reply
              0
              • SGaistS SGaist

                Might be a silly question but are you sure that the counter and maximum values are accurate ?

                K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                @SGaist

                Yes, that is one of the routines sending the signals.
                The principle outline of the other is similar.

                void SplitCopyJobs::copy ( const QFileInfoList & lstintrvl, const QString & oldBase,  const QString & newJob )
                {
                    int maxCnt = lstintrvl.size();
                    sigCopying ( 0, maxCnt );
                    int cnt = 0;
                    for ( QFileInfoList::const_iterator it = lstintrvl.begin(); it != lstintrvl.end(); ++it )
                    {
                        QString oldName = it->absoluteFilePath();
                        QString newName = newJob + oldName.mid ( oldBase.size() );
                        QString targetPath = QFileInfo ( newName ).absolutePath();
                        QDir tdir ( targetPath );
                        if ( !tdir.exists() )
                        {
                            tdir.mkpath( targetPath );
                        }
                        QFile::copy( oldName, newName );
                        sigCopying ( ++cnt, maxCnt );
                    }
                }
                

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

                kshegunovK JonBJ 2 Replies Last reply
                0
                • K koahnig

                  @SGaist

                  Yes, that is one of the routines sending the signals.
                  The principle outline of the other is similar.

                  void SplitCopyJobs::copy ( const QFileInfoList & lstintrvl, const QString & oldBase,  const QString & newJob )
                  {
                      int maxCnt = lstintrvl.size();
                      sigCopying ( 0, maxCnt );
                      int cnt = 0;
                      for ( QFileInfoList::const_iterator it = lstintrvl.begin(); it != lstintrvl.end(); ++it )
                      {
                          QString oldName = it->absoluteFilePath();
                          QString newName = newJob + oldName.mid ( oldBase.size() );
                          QString targetPath = QFileInfo ( newName ).absolutePath();
                          QDir tdir ( targetPath );
                          if ( !tdir.exists() )
                          {
                              tdir.mkpath( targetPath );
                          }
                          QFile::copy( oldName, newName );
                          sigCopying ( ++cnt, maxCnt );
                      }
                  }
                  
                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  If you don't offload that to a thread, or run the for over the event loop you're not giving a chance for any events to be processed.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  2
                  • K koahnig

                    @SGaist

                    Yes, that is one of the routines sending the signals.
                    The principle outline of the other is similar.

                    void SplitCopyJobs::copy ( const QFileInfoList & lstintrvl, const QString & oldBase,  const QString & newJob )
                    {
                        int maxCnt = lstintrvl.size();
                        sigCopying ( 0, maxCnt );
                        int cnt = 0;
                        for ( QFileInfoList::const_iterator it = lstintrvl.begin(); it != lstintrvl.end(); ++it )
                        {
                            QString oldName = it->absoluteFilePath();
                            QString newName = newJob + oldName.mid ( oldBase.size() );
                            QString targetPath = QFileInfo ( newName ).absolutePath();
                            QDir tdir ( targetPath );
                            if ( !tdir.exists() )
                            {
                                tdir.mkpath( targetPath );
                            }
                            QFile::copy( oldName, newName );
                            sigCopying ( ++cnt, maxCnt );
                        }
                    }
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @koahnig
                    I think, just as @kshegunov says, that although you are delivering sigCopying you are not giving any call for the event loop to be processed, to act on the signal to update the progress bar? You are supposed to call something like QApplication.processEvents() regularly during a "long-running operation" to allow events to be processed and your bar to get updated?

                    1 Reply Last reply
                    1
                    • K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #10

                      @kshegunov @JonB

                      Thanks for confirming my suspision I expressed already in my initial post

                      @koahnig said in Using two QProgressBar in MainWindow:

                      Basically the whole copying is done in one slot of the main window. Would I need to shift that into a separate thread?
                      Is not really logical because that worked and suddenly stopped.

                      However, it is really confusing when something works and suddenly not completely correct. I was not sure if there is some "special magic" somewhere.

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

                      kshegunovK 1 Reply Last reply
                      0
                      • K koahnig

                        @kshegunov @JonB

                        Thanks for confirming my suspision I expressed already in my initial post

                        @koahnig said in Using two QProgressBar in MainWindow:

                        Basically the whole copying is done in one slot of the main window. Would I need to shift that into a separate thread?
                        Is not really logical because that worked and suddenly stopped.

                        However, it is really confusing when something works and suddenly not completely correct. I was not sure if there is some "special magic" somewhere.

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

                        @koahnig said in Using two QProgressBar in MainWindow:

                        However, it is really confusing when something works and suddenly not completely correct.

                        Indeed, although the confusing part is that it doesn't really work, only gives the appearance of working, I get that a lot with my code. ;)

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