Qt/C++ How can I disconnect a QProgressDialog::canceled signal to its QProgressDialog::cancel slot?

785 views Asked by At

I have a QProgressDialog and I would like to override its cancel() slot to change its behavior.

Instead of closing the dialog, I would like to do some other operations and then close the dialog after a QThread to finish before closing the dialog.

I tried to disconnect the canceled/cancel signal/slot couples and the reconnect with the new behavior but it does not seem to change much.

As as soon as I click on the cancel button, the progress dialog gets closed first and then my lambda get executed anyway.

Qobject::disconnect(m_progressdialog, &QProgressDialog::canceled, m_progressdialog, &QProgressDialog::cancel);

Qobject::connect(m_progressdialog, &QProgressDialog::canceled, [](){
  // continue displaying the dialog as an occupation bar
  m_progressdialog->setValue(0);
  // do some other things
  // a lot of code
  // ...
  // only later close the dialog
  m_progressdialog->close();
});

Is there a way to do this correctly?

2

There are 2 answers

0
Tarod On BEST ANSWER

I don't know your whole code, but according to the documentation, the idea is more or less the same you're saying: a slot to connect the signal QProgressDialog::canceled().

The following code is just an example but it's working. In this case, instead of using the own Qt property wasCanceled, it is used a boolean to control when to stop and cancel the QProgressDialog.

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

class QProgressDialog;

QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

private slots:
    void on_pushButton_clicked();
    void my_custom_cancel();

private:
    Ui::Dialog *ui;
    QProgressDialog *progress;
    int numTasks = 100000;
    bool canceled = false;
};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

#include <QProgressDialog>
#include <QThread>
#include <QDebug>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    progress = new QProgressDialog("Task in progress...", "Cancel", 0, numTasks);

    connect(progress, SIGNAL(canceled()), this, SLOT(my_custom_cancel()));

    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pushButton_clicked()
{
    progress->open();

    for (int i = 0; i < numTasks; i++) {
        progress->setValue(i);
        QThread::usleep(100);
        if (canceled)
            break;
    }
    progress->setValue(numTasks);
}

void Dialog::my_custom_cancel()
{
    qDebug() << "do something";

    canceled = true;
}
0
Cwift On

Although it's 2020's question, my code ran into the same problem and deal it out.

I change the code fromdisconnect(this, &QProgressDialog::canceled, this, &QProgressDialog::cancel); to disconnect(this, &QProgressDialog::canceled, nullptr, nullptr); and it disconnect succeed.

Hope it can help people who read this and can someone explatin why the first code didn't work?