Question

I'm using QT to implement some UI program. In this program I need a progress dialog. I tried to use the build-in QProgressDialog, it works fine but in my case I need to confirm (with another dialog) when the "cancel button" is clicked.

In QProgressDialog once the cancel button is clicked, the progress dialog will be canceled, so, I tried to implement my own progress dialog (very simple, a dialog with progress bar). However, if I use my own progress dialog, there is some problems. It cannot be moved or clicked. Once I tried to move it and the dialog loss its focus, the progress bar won't update any more and it cannot gain the focus again. I tried to set different Modality, but either Qt::ApplicationModal or Qt::WindowModal has the same situation.

follows is my progress dialog class, if someone knows how to modify QProgressDialog to meet the confirm requirement or where is the problem in my code.

header:

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

    void setRange(int minimum, int maximum);
    void setValue(int value);
    void setLabelText(QString labtext);
    bool wasCanceled();

private:
    Ui::Dialog *ui;
    bool cancelStatus;

private slots:
    void cancel();
};

Source:

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

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    cancelStatus = false;
    ui->progressBar->setRange(0,1);
    ui->progressBar->setValue(0);
    //this->setWindowModality(Qt::WindowModal);
    show();
}

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

void Dialog::setRange(int minimum, int maximum){
    ui->progressBar->setRange(minimum,maximum );
}

void Dialog::setValue(int value){
    this->ui->progressBar->setValue(value);
}

void Dialog::setLabelText(QString labtext){
    this->ui->label->setText(labtext);
}

void Dialog::cancel(){
// pop up the confirm dialog here
// cancelStatus = true if the confirm dialog is accepted, else do nothing .
}

bool Dialog::wasCanceled(){
    return cancelStatus;
}
Was it helpful?

Solution

From the Qt Documentation: The signal QProgressDialog::canceled() is emitted when the cancel button is clicked and it is connected to the cancel() slot by default.

Did you try to connect the canceled signal to you own validation slot, and the cancel the dialog if the user confirm is choice?

Before, connecting your own slot, disconnect the canceled signal from the cancel slot using QObject::disconnect() : http://doc.qt.io/archives/qt-4.7/qobject.html#disconnect

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top