Can anyone shed some brighter light on this topic please?
I'd suggest that the way to do this is not to build the tree in the dialog constructor. A common "trick" is to start a single-shot QTimer with 0 ms timeout at the very end of the constructor. Connect the timeout() signal of the timer to a slot in your dialog class. In this slot, build your tree. The net effect of this trick is that the timer's timeout signal won't be handled until after the dialog is displayed (with an empty tree, of course). While you are building your tree, you can use a progress dialog to keep the user informed.
MyDialog
::MyDialog( QWidget * parent
){
// setupUi(); etc.
QTimer::singleShot( 0,
this, buildTree
() );
}
void MyDialog::buildTree()
{
int nItems = totalItemsToBeAdded;
progress.setWindowModality(Qt::WindowModal);
for ( int nItem = 0; nItem < nItems; ++nItem )
{
progress.setValue( nItem );
// add tree widget item
}
// Maybe emit a signal here if needed, or just let the user start interacting with the tree
}
MyDialog::MyDialog( QWidget * parent )
: QDialog( parent )
{
// setupUi(); etc.
QTimer::singleShot( 0, this, buildTree() );
}
void MyDialog::buildTree()
{
int nItems = totalItemsToBeAdded;
QProgressDialog progress( "Building tree", QString(), 0, nItems, this (;
progress.setWindowModality(Qt::WindowModal);
for ( int nItem = 0; nItem < nItems; ++nItem )
{
progress.setValue( nItem );
// add tree widget item
}
// Maybe emit a signal here if needed, or just let the user start interacting with the tree
}
To copy to clipboard, switch view to plain text mode
Alternatively, if you have real estate available in your dialog, you can simply put a QProgressBar in it and use that to show the load progress. After the tree is built, you can simply hide() the progress bar.
Another slightly more complex scenario if you don't have the real estate is to use a stack widget in your dialog, one page containing the tree, the other containing the progress bar. When the dialog first comes up, the page containing the progress bar is shown; once the tree is filled, switch to the page containing the tree. This might actually cause filling the tree to be faster since it isn't visible and thus won't be constantly trying to update itself. It will simply update once, when you make it visible by switching pages.
Bookmarks