QProgressBar (Qt 5.4.0) produces black blank screen

746 views Asked by At

I wanted to use a QProgressBar in the following function:

void InstallingWindow::install_package(QString pkgname, QString tempdir){

    qDebug() << "Imported tempdir is " + tempdir;

    QFile ee_script("/usr/bin/ee_script");

    QString program_install = "sudo /usr/bin/ee_script " + pkgname + " " + tempdir + " install";

    if (!ee_script.exists()){
        qDebug() << "Install script does not exists";
        ee_script.error();
    }
    else{
        process_install->start(program_install);
        ui->progressBar->setMinimum(0);
        ui->progressBar->setMaximum(0);

        connect(process_install, SIGNAL(finished(int, QProcess::ExitStatus)), ui->progressBar, SLOT(show_progress_bar()));  
        process_install->waitForFinished(-1); //this will make the screen blank

    ui->progressBar->setMaximum(0);
    ui->progressBar->setValue(100);

        ui->nextButton->setEnabled(true);

    }
    post_install();

}

void InstallingWindow::show_progress_bar(){

    ui->progressBar->setMaximum(100);
    ui->progressBar->setValue(100);
    ui->nextButton->setEnabled(true);

}

I wanted progress bar to work as a wait bar (initially; later will be implemented with timer) and completed (finished at 100) after the QProcess finished. I get a blank black screen with process_install->waitForFinished(-1). I tried many options but all in vain, while QProgressDialog works fine. Kindly help me where is the fault and what could I do.

1

There are 1 answers

3
Toby Speight On BEST ANSWER

When you waitForFinished, you are blocking the GUI thread, so the progress dialog never gets events, and so never paints. You'll need to arrange for the event loop to run regularly - something like (untested):

process_install->start(program_install);

do {
    qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
} while (process_install->waitForFinished(20)); //  1/50 second

ui->nextButton->setEnabled(true);

BTW, it's good idea to connect(process_install, SIGNAL(...), ...) before process_install->start, to avoid race conditions. Unlikely with a process that needs a progress meter, but still a good habit to get into. :-)