PDA

View Full Version : QProgressDialog Not Showing



ToddAtWSU
11th March 2013, 13:51
I have a function that allows the user to open multiple files. These files are basic ASCII text files but can be multiple megabytes long containing a lot of information that unfortunately takes a little time to load. Depending on the number of files they open, it can take upwards of a minute. I want to assure the user the program hasn't hung by using a QProgressDialog to show them stuff is happening. However, the QProgessDialog never shows until the final couple seconds and is fairly useless. I thought it was the 4-second delay the dialog has, but it will take longer than 4 seconds for it to show and I have tried changing the minimum duration but it still doesn't work. Here is my code, can you please tell me where I am making a mistake. Sorry for any typos in my code, it resides on a standalone network so I am typing this back in by hand from a printout.



void MainWindow::openFiles( )
{
QStringList filenames = QFileDialog::getOpenFileNames( ... )
int numFiles = filenames.size( );
QProgressDialog progress( "Opening Files...", "Cancel", 0, numFiles, this );
progress.setWindowModality( Qt::WindowModal );
progress.setMinimumDuration( 0 );
progress.setValue( 0 );
progress.raise( ); // I have also tried progress.show( )

foreach( QString filename, filenames )
{
progress.setLabelText( QString( "Opening file %1 of %2 ).arg( progress.value( ) + 1 ).arg( numFiles ) );
openFile( filename ); // Code that does the reading in and storing of the file

if( progress.wasCanceled( ) )
{
break;
}

progress.setValue( progress.value( ) + 1 );
}

progress.setValue( progress.maximum( ) );
}


Thanks for your help. I am running this on Qt 4.7.3 on Solaris 10. Of note, the input window from QFileDialog::getOpenFileNames shows me the gray outline from this dialog...so it's not redrawing the QMainWindow right away when the user closes the dialog. I don't know if this is causing some of the issues which is why I tried calling raise( ) on the QProgressDialog.

Thanks again!
Todd

Lesiok
11th March 2013, 14:09
The problem well-known and described under the heading "qt gui responsive". Ask Mr Google.

anda_skoa
11th March 2013, 15:49
You are not allowing the main thread to do any event processing because you stay in a tight loop and do not return to the event loop.
Thus it cannot do any process any updates, neither for your main UI nor for the progress dialog.

If loading of a single file is fast enough you can call QCoreApplication::processEvents() in between files, ideally later on refactoring it to process files from a queue with a slot that is called through delayed invocation for every run.

If loading of a single file can take longer than a user would be comfortable with, then you'll either need to do event process in the loading code or move loading to a thread.

Cheers,
_