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. QProgressDialog::setValue Crash
QtWS25 Last Chance

QProgressDialog::setValue Crash

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 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.
  • joeQJ Offline
    joeQJ Offline
    joeQ
    wrote on last edited by
    #1

    In MainWindow, I have the below code snippet.

    void MainWindow::Function_1()
    {
        /** some code here */
    
        int threadNum = 1000;///< just code snippet
        if(0 == _progressDlg){
            ///< _progressDlg MainWindow class data member.
            _progressDlg = new QProgressDialog(this);
            _progressDlg->setLabelText("Label text");
            _progressDlg->setRange(0,threadNum);
        }
    
        for(int i=0; i < subNum; i++){
            EngineTask* task = new EngineTask(...);///< 
            connect(task,&EngineTask::SigEngineTaskFinished,
                    this,&MainWindow::StEngineTaskFinished);
            _threadPool->start(task);
        }
    }
    

    I want to use the QProgressDialog to indicate the progress had done of my tasks.

    EngineTask class
    /** some include */
    
    class EngineTask : public QObject, public QRunnable
    {
        Q_OBJECT
    public:
        EngineTask(...);
        ~EngineTask();
    
        void run() override; ///< will emit SigEngineTaskFinished(...), in run function
    
    signals:
        void SigEngineTaskFinished(...);
    
    private:
        /** some class members */
    };
    
    
    MainWindow::StEngineTaskFinished

    In the slot, I update the value of QProgressDialog, But, some time, it can crashed at here.

    void MainWindow::StEngineTaskFinished(...)
    {
        /** some code here */
    
        if(0 != _progressDlg){
            int val = _progressDlg->value();
            val++;
            _progressDlg->setValue(); ///< ??? some time crash at here.
        }
    }
    

    I see the Qt help manual.

    Warning: If the progress dialog is modal (see QProgressDialog::QProgressDialog()), setValue() calls QApplication::processEvents(), so take care that this does not cause undesirable re-entrancy in your code. For example, don't use a QProgressDialog inside a paintEvent()!

    I don't clear it well now.

    1. How to work it out?
    2. what's best way?

    Just do it!

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

      Hi,

      Any chance you forgot to initialise _progressDlg to 0 in your constructor ? Or you should rather use nullptr.

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

      joeQJ 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Any chance you forgot to initialise _progressDlg to 0 in your constructor ? Or you should rather use nullptr.

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #3

        @SGaist

        I didn't forgot to initialise my class members.

        Just do it!

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

          What does the stack trace tell you about the crash ?

          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
          • joeQJ joeQ

            In MainWindow, I have the below code snippet.

            void MainWindow::Function_1()
            {
                /** some code here */
            
                int threadNum = 1000;///< just code snippet
                if(0 == _progressDlg){
                    ///< _progressDlg MainWindow class data member.
                    _progressDlg = new QProgressDialog(this);
                    _progressDlg->setLabelText("Label text");
                    _progressDlg->setRange(0,threadNum);
                }
            
                for(int i=0; i < subNum; i++){
                    EngineTask* task = new EngineTask(...);///< 
                    connect(task,&EngineTask::SigEngineTaskFinished,
                            this,&MainWindow::StEngineTaskFinished);
                    _threadPool->start(task);
                }
            }
            

            I want to use the QProgressDialog to indicate the progress had done of my tasks.

            EngineTask class
            /** some include */
            
            class EngineTask : public QObject, public QRunnable
            {
                Q_OBJECT
            public:
                EngineTask(...);
                ~EngineTask();
            
                void run() override; ///< will emit SigEngineTaskFinished(...), in run function
            
            signals:
                void SigEngineTaskFinished(...);
            
            private:
                /** some class members */
            };
            
            
            MainWindow::StEngineTaskFinished

            In the slot, I update the value of QProgressDialog, But, some time, it can crashed at here.

            void MainWindow::StEngineTaskFinished(...)
            {
                /** some code here */
            
                if(0 != _progressDlg){
                    int val = _progressDlg->value();
                    val++;
                    _progressDlg->setValue(); ///< ??? some time crash at here.
                }
            }
            

            I see the Qt help manual.

            Warning: If the progress dialog is modal (see QProgressDialog::QProgressDialog()), setValue() calls QApplication::processEvents(), so take care that this does not cause undesirable re-entrancy in your code. For example, don't use a QProgressDialog inside a paintEvent()!

            I don't clear it well now.

            1. How to work it out?
            2. what's best way?
            joeQJ Offline
            joeQJ Offline
            joeQ
            wrote on last edited by
            #5

            @joeQ

            At the moment i do not know waht causes this crash.

            I tried many ways to work it out. not, i find one way.

            I test this way, not has the crash.

            my way

            In my mainwindow.h,

                /** MainWindow class data members */
                int              _progressVal   = 0;
                QTimer*          _progressTimer = 0;
                ProgressDialog*  _progressDlg   = 0;
            
                QThreadPool*     _threadPool    = 0;
            

            In MainWindow construct function

                /** some code */
            
                _progressTimer = new QTimer(this);
                _progressTimer->setInterval(1000);
                connect(_progressTimer,&QTimer::timeout,
                        this,&MainWindow::StProgressTimerOut);
            
                /** some code */
            

            About the StProgressTimerOut

            void MainWindow::StProgressTimerOut()
            {
                if(0 == _progressDlg){
                    _progressTimer->stop();
                    return;
                }
            
                _progressDlg->setValue(_progressVal); ///< In here to update progressbar value
                if(_threadPool->activeThreadCount() == 0){
                    _progressTimer->stop();
                    _progressDlg->deleteLater();
                    _progressDlg = 0;
                    /** ... */
                }
            }
            

            So, When begin to start thread

                /** ... */
                int subNum = 100;
                for(int i=0; i < subNum; i++){
                    task = new EngineTask(...);
                    connect(task,&EngineTask::SigEngineTaskFinished,
                            this,&MainWindow::StEngineTaskFinished,Qt::QueuedConnection);
                    _threadPool->start(task);
                }
            
                if(0 == _progressDlg){
                    _progressDlg = new ProgressDialog(tr("Label text"),subNum,this);
                    _progressTimer->start(500);
                    _progressVal = 0;
                }
            

            In, the Slot function

                /**...*/
                _progressVal++;///< Not in slot to update progressbar value, update it in timer out slot function.
                /**...*/
            

            Just do it!

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

              Why don't you create your dialog directly since you'll obviously use it ?
              That would avoid all these existence checks all over the place.

              Otherwise, it would also make more sense to create it before you generate your subNum tasks.

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

              joeQJ 1 Reply Last reply
              0
              • SGaistS SGaist

                Why don't you create your dialog directly since you'll obviously use it ?
                That would avoid all these existence checks all over the place.

                Otherwise, it would also make more sense to create it before you generate your subNum tasks.

                joeQJ Offline
                joeQJ Offline
                joeQ
                wrote on last edited by
                #7

                @SGaist yes, i will modify my code after. thank you very much.

                Just do it!

                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