1

I'm creating the new window with QProgressBar when i click on button of MainWindow but when new window is creating, QProgressBar don't appear while filling cycle is working. After then QProgressBar appear and it is filled.

Constructor:

ProgressWin::ProgressWin():QWidget()
{
    this->resize(273,98);
    this->move(670, 430);
    bar1 = new QProgressBar(this);
    bar1->setGeometry(20, 31, 251, 31);
    bar1->setMinimum(0);
    bar1->setMaximum(10000);
    this->show();
    unsigned long long secr, PQ;
    unsigned long long rv;
    unsigned long long decr;
    for(int v = 0; v <= 100000; v++) {
            bar1->setValue(v);
    }
}

Code of button that call new window:

void RsaMainWindow::ButtClickCrypt()
{
    FileName1 = ui->LineCrypt->text();
    if(FileName1.isEmpty()) {
        QMessageBox::information(0, "Information", "File for Crypt wasn't chosen");
        return;
    }
    NewWin = new ProgressWin;
}

Class for new window:

class ProgressWin : public QWidget
{
    QProgressBar *bar1;
public:
    ProgressWin();
};

Class for MainWindow:

[namespace Ui {
class RsaMainWindow;
}

class RsaMainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void ButtClickViewCryp();
    void ButtClickViewDecr();
    void ButtClickViewKeys();
    void ButtClickCrypt();
    void ButtClickDecr();

private:
    Ui::RsaMainWindow *ui;
    QString FileName1;
    QString FileName2;
    QString FileName3;
    ProgressWin *NewWin;

};][1]
  • Why would you expect the progress bar to fill up visibly if you are filling it up in a for-loop that doesn't return control to the GUI to make its updates? – UnholySheep Sep 15 '16 at 11:26
  • 2
    You should familiarize yourself with GUIs a bit more and read about the event handling loop etc. – Hayt Sep 15 '16 at 11:36

1 Answers1

0

User interface usually work on the event-loop principle:

While (not closing the app)
    Wait for some event
    update app according event
endWhile

If you implement your heavy task in the GUI thread, when the user click on "Perform a heavy task", the code managing this click is called, and after it finish, a following event will trigger the drawing of the window. That mean your heavy task will freeze the user interface during the task.

To perform a heavy task correctly, you need to:

  • Create a background thread that perform the task. Each iteration, it update some shared-memory (or equivalent) status of the task. Some UI libraries, like QT allows to send queued messages, which help for those cases.
  • On the Main thread, on update of the status, set the progress bar to the new value and return.
Adrian Maire
  • 14,354
  • 9
  • 45
  • 85