Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QProgressDialog not displayed



  • Hi,
    I have a TaskManager that has to perform some long operation. Since the operation has not a fixed duration I want a QProgressDialog to be displayed with an undefined progress bar.
    Below is my code but it does not work: the dialog is not displayed. Where am I doing wrong?

    auto response = QMessageBox::question(this, "Confirm", "Are you sure you want to install?");
    if (response == QMessageBox::StandardButton::Yes) 
    {
        QProgressDialog progress("Installing...", "Abort", 0, 0, this);
        progress.setWindowModality(Qt::WindowModality::WindowModal);
        progress.show();
    
        QThread *thread = new QThread(this);
        TaskManager *task = new TaskManager();
        task->moveToThread(thread);
    
        connect(task,   &TaskManager::installed, task,      &TaskManager::deleteLater);
        connect(task,   &TaskManager::installed, thread,    &QThread::quit);
        connect(task,   &TaskManager::installed, &progress, &QProgressDialog::close);
        connect(thread, &QThread::started,       task,      &TaskManager::install);
        connect(thread, &QThread::finished,      thread,    &QThread::deleteLater);
        
        thread->start();
    }
    

  • Lifetime Qt Champion

    Hi
    Best guess is that the variable
    QProgressDialog progress;
    is deleted very soon as the function ends.
    so even thread lives, the actual QProgressDialog does not.

    try
    QProgressDialog *progress = new QProgressDialog("Installing...", "Abort", 0, 0, this);



  • @mrjj said in QProgressDialog not displayed:

    Hi
    Best guess is that the variable
    QProgressDialog progress;
    is deleted very soon as the function ends.
    so even thread lives, the actual QProgressDialog does not.

    try
    QProgressDialog *progress = new QProgressDialog("Installing...", "Abort", 0, 0, this);

    Thanks it works! It was a very simple solution.


Log in to reply