1

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;
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
Claire Huang
  • 961
  • 1
  • 18
  • 30
  • I would subclass QProgressDialog, then just call another dialog when the cancel button is clicked. QMessageDialog might be a good candidate. – Jay Nov 06 '10 at 05:09
  • Hi James: That's what I do before. However, the problem is that the "cancel" signal is emitted once the cancel button is clicked and the dialog will be canceled, as I mentioned before. What I want is that it will only be canceled after the confirmation. – Claire Huang Nov 06 '10 at 18:12

1 Answers1

2

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

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Symbiosoft
  • 4,681
  • 6
  • 32
  • 46
  • yes, I tried~~I can connect "cancel" signal to my own slot, however, the real problem is that I cannot remove the build-in connection of QProgressDialog since I inherit it. Once the cancel signal is emitted, both my slot and the build-in slot will be called and so that the dialog will be canceled. – Claire Huang Nov 08 '10 at 06:46
  • QObject has a disconnect method. Did you try to disconnect the canceled signal from the cancel slot before connecting it your own slot? http://doc.qt.nokia.com/4.7/qobject.html#disconnect – Symbiosoft Nov 08 '10 at 13:54
  • Thanks!! I found that the problem is I try to "overwrite" the cancel slot, after I change the name, I can correctly disconnect the slot :D! – Claire Huang Nov 09 '10 at 06:36