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

QDialog doesn't render properly



  • Hello all,

    I have coded a status dialog for my project. The status dialog is supposed to be a small modal window with a status message label that appears when the application is going to do some time-consuming process. Its purpose is, quite obviously, to inform the user that the application is working on something and to block the user from doing anything on the application during that time. I coded the functions below but the problem is that the dialog appears empty while the label remains unrendered until the process is done but then the dialog is closed anyway as it's supposed to.

    Here are the functions to show and hide:
    @
    void MainWindow::showStatus(QString msg) {

    if (this->isDialogShown == true) {
        hideStatus();
    }
    
    // Create the dialog and set its settings
    this->statusDialog = new QDialog( this );
    this->statusDialog->setWindowOpacity( 0.72 );
    this->statusDialog->setWindowFlags( Qt::SplashScreen );
    this->statusDialog->setWindowModality( Qt::WindowModal );
    this->statusDialog->setFixedSize(220, 80);
    this->statusDialog->setEnabled( false );
    
    // Fix a layout for it
    this->statusDialog->setLayout( new QVBoxLayout() );
    
    // Add the label with the status message
    QLabel *label = new QLabel( this->statusDialog );
    label->setAlignment( Qt::AlignCenter );
    label->setFont( QFont("MS Shell Dlg 2", 15, QFont::Bold, true) );
    label->setText( msg );
    this->statusDialog->layout()->addWidget( label );
    
    // Show the dialog
    this->statusDialog->show();
    
    // Set flag
    this->isDialogShown = true;
    

    }

    void MainWindow::hideStatus() {

    // Close window and unset flag
    this->statusDialog->hide();
    this->isDialogShown = false;
    

    }
    @

    I use the function as such:
    @
    ...
    void MainWindow::someFunction() {
    showStatus( "Processing..." );

    someTimeConsumingFunction();
    
    hideStatus();
    

    }
    ...
    @

    How can I force the dialog to render everything before the processing starts?

    Thanks in advance!


  • Lifetime Qt Champion

    Hi,

    You would probably need to call processEvents() but why don't you use a QProgressDialog ? It's purpose corresponds to your use case

    Hope it helps


Log in to reply