Dialog displayed late
-
Hi, I've a problemi with a Dialog.
I've this code:QString ImportExportMap::ImportMap(QStringList *InfoData) { QString FileName; FileName = QFileDialog::getOpenFileName(this, "MAP File", cMapArchive, "File map (*.map)"); if(FileName.isEmpty()) return NULL; QFileInfo f(FileName); FileName = f.fileName(); QDialog *Dlg = new QDialog; Dlg->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog); QHBoxLayout *h = new QHBoxLayout; Dlg->setLayout(h); QLabel *l = new QLabel("Loading file, please wait..."); h->addWidget(l); Dlg->show(); QFile InputFile(cMapArchive+"/"+FileName); if (InputFile.open(QIODevice::ReadOnly)) { QTextStream in(&InputFile); while (!in.atEnd()) { ... } } InputFile.close(); Dlg->close(); delete Dlg; return FileName; }
The problem is that Dlg is shown late, when the file has already been loaded.
How I can starts with loading file only if Dlg is completely shown?
Thanks. -
@Stefanoxjx said in Dialog displayed late:
How I can starts with loading file only if Dlg is completely shown?
Split your function into two separate ones. The first one ends at
Dlg->show();
. The remainder of your code should be in a function called from theQDialog::showEvent()
method, which is called when a widget is actually shown. You will need to subclassQDialog
for your dialog to achieve that, so that you canoverride
that method. You might have your overridden method emit a signal, so that yourImportExportMap
class can put a slot on that and you can keep the code in theImportExportMap
class rather than having to put it in the sub-classed dialog code, as you please.Given your usage, a rather dirty/naughty way may be to put
QCoreApplication::processEvents()
after your currentDlg->show();
. That might be enough to just show the dialog initially, I'm not sure.Note that Qt has a
QProgressDialog
, or even aQProgressBar
class, which you might prefer to use, since all you seem to want the dialog for is to inform the user while loading/parsing the file, and you may find simpler/preferable to the above. -
Hi JonB, thanks for your help.
I used QDialog::showEvent :)