Can't Cancel Progress Dialog.



  • I have a progress dialog set up as follows:

    connect(myProgressDialog, signal(canceled()), this, SLOT(cancelProgress));
    connect(myProgressTimer, signal(timeout()), this, SLOT(updateProgress));
    
    cancelProgress
    (
            myProgressDialog->cancel();
            myProgressTimer->stop();
            myProgressStep = 0;
    )
    
    updateProgress()
    (
        if (myProgressDialog->wasCanceled())
        (
             #Do Something.
        )
        ...
    )
    

    If I press the cancel button or the X (Close) button, cancelProgress doesn't get called and wasCanceled is not true in Update Progress. The fascinating thing is that when those buttons are pressed the dialog pops down and pops back up. It appears that when they are pressed they receive a signal, just not the signal I pass it.

    Any ideas?


  • Lifetime Qt Champion

    Hi,

    Is it a typo here or do you really have signal in place of SIGNAL in your code ?



  • It's a Typo.


  • Lifetime Qt Champion

    Can you show the complete code where you are using that dialog ?



  • It comes from a separate network. Let see if I can get it off.



  • myProgressTimer = new QTimer(this);

    ...

    connect(myProgressDialog, signal(canceled()), this, SLOT(cancelProgress));
    connect(myProgressTimer, signal(timeout()), this, SLOT(updateProgress));

    ...
    myProgressTimer ->setRange(0, 100);
    myProgressTimer ->start(1000);

    ...

    cancelProgress
    (
    myProgressDialog->cancel();
    myProgressTimer->stop();
    myProgressStep = 0;
    )

    updateProgress()
    (
    if (myProgressDialog->wasCanceled())
    (
    #Do Something.
    )
    myProgressDialog-<setValue(myProgressStep++);

    if (myProgressStep > myProgressDialog->maximum())
    {
        myProgressDialog->cancel(); //This Works!
        
        if (myProgressTimer)
       {
           myProgressTimer ->stop(); //This Works!          
       }
    
       myProgressStep = 0;
    

    }



  • Hi @DougyDrumz

    After testing your code cancelProgress slot is called when cancel button is clicked:

    But you are telling :

    If I press the cancel button or the X (Close) button,

    AFAIK canceled signal of progressDialog is just emitted when cancel button is clicked, not when X (Close) button,

    see the doc:

    http://doc.qt.io/qt-5/qprogressdialog.html#canceled

    This signal is emitted when the cancel button is clicked. It is connected to the cancel() slot by default.

    Here is the code that work when cancel button is clicked:

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QProgressDialog>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void cancelProgress();
        void updateProgress();
    
    private:
        Ui::MainWindow *ui;
        QProgressDialog* myProgressDialog;
        QTimer* myProgressTimer;
        int myProgressStep;
    };
    
    #endif // MAINWINDOW_H
    

    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QTimer>
    #include <QProgressDialog>
    
    #include <QDebug>
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    
    
        myProgressTimer = new QTimer(this);
    
        myProgressDialog= new QProgressDialog(this);
    
        ui->verticalLayout->addWidget(myProgressDialog);
    
        connect(myProgressDialog, SIGNAL(canceled()), this, SLOT(cancelProgress()));
        connect(myProgressTimer, SIGNAL(timeout()), this, SLOT(updateProgress()));
    
        myProgressTimer->start(1000);
        myProgressStep = 0;
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::cancelProgress()
    {
        qDebug() << myProgressDialog->wasCanceled() << "<==canceled?";
        qDebug() << Q_FUNC_INFO << "i am cancelled";
        myProgressDialog->cancel();
        myProgressTimer->stop();
    }
    
    void MainWindow::updateProgress()
    {
        myProgressDialog->setValue(myProgressStep++);
        if (myProgressDialog->wasCanceled())
        {
           // do something
        }
    }
    

    And here is the ouput code when cancel button is clicked:

    true <==canceled?
    void MainWindow::cancelProgress() i am cancelled

    Hope this can help !



  • cancelProgress doesn't get called for me, neither when hitting the Cancel button nor the Close button.



  • @DougyDrumz said in Can't Cancel Progress Dialog.:

    cancelProgress doesn't get called for me, neither when hitting the Cancel button nor the Close button.

    Have you tried with the code that i shared on the previous post?

    You might have error on syntax of your connect

    connect(myProgressDialog, signal(canceled()), this, SLOT(cancelProgress));
    

    Try with my code , i know that it work 100%



  • I'm doing the same thing you're doing (re the ProgressDialog). There is no syntax error. The interesting thing is that when I push the Cancel Button, it does pop down and pop right back up. How does it do this if cancelProgress is not called. What is it calling? Also interesting is that the close (X) buitton is pressed, it does cancel, buit only on the second attempt. I changed the QTimer start from 1000 to 100 to 1 and it didn't change anything. What the heck is going on here? I have another qtimer to update a clock. Could this be interfering with the Progress QTimer?



  • OK. I found it. I was being dumb! I have two flavors of Progress Dialogs. One is inside an App, and the other is standalone, meant to be called form a script. I was looking at the one in the App when I should have been looking at the other one. The standalone one doesn't have have a cancel slot.



  • @DougyDrumz said in Can't Cancel Progress Dialog.:

    OK. I found it. I was being dumb! I have two flavors of Progress Dialogs. One is inside an App, and the other is standalone, meant to be called form a script. I was looking at the one in the App when I should have been looking at the other one. The standalone one doesn't have have a cancel slot.

    Good !



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.