Qt MainWindow how to close and reopen
-
I've added an import function to my application that imports an SQL file into MariaDB, this works ok, I want to cause the dialog that the import button is present on to reopen causing the table view to repopulate with the database records.
Is this possible with existing signals?
-
@jsulm , what I want to do is trigger all the initial checks that are performed when the window is first opened. Presently, when I click the import button it imports the SQL file into the database and this works fine, the dialog doesn't display the data, if I close the dialog then open it again, the data is displayed.
What I want to do is trigger these actions automatically after the import completes so the dialog shows the data automatically.
-
@SPlatten said in Qt MainWindow how to close and reopen:
What I want to do is trigger these actions automatically after the import completes so the dialog shows the data automatically.
Well, I don't know your code, so can't say why it does not show the data in the dialog. You will need to show your code.
-
Closing and re-launching your entire main window in your case seems too much of the good thing to me. Without knowing your code architecture, I can only guess that you populate a widget or more widgets with your database content. You can just write a private method or a slot that clears the widget(s) and populates them. After your import, you call the private method or emit a signal connected to your slot. No need to close and restart your main window with the user loosing size and margins.
-
@jsulm , I import the file using the following:
//Show busy cursor QApplication::setOverrideCursor(Qt::WaitCursor); //Build import command line QString strApp(DataSets::mscszMySQL); QStringList slstArguments; slstArguments << (QString(DataSets::mscszOptionHost) + Trainer::strHost()) << (QString(DataSets::mscszOptionUser) + Trainer::strUser()) << (QString(DataSets::mscszOptionPassword) + Trainer::strPassword()) << QString(DataSets::mscszOptionComments); QProcess* pobjProc(new QProcess(this)); if ( pobjProc != nullptr ) { QObject::connect(pobjProc, &QProcess::errorOccurred, [pobjProc](QProcess::ProcessError eError) { qdbg() << tr("Import error: ") << eError; } ); strApp = strDBinstFolder + strApp; pobjProc->setWorkingDirectory(strDBinstFolder); pobjProc->setStandardInputFile(strImportFile); pobjProc->start(strApp, slstArguments); pobjProc->waitForFinished(); } //Update the main dialog emit closeAndReopen(); //Restore cursor QApplication::restoreOverrideCursor();
There are several issues here which I would like to resolve:
- No wait cursor is displayed, despite the call at the start.
- The line: emit closeAndReopen is connected to my own slot which I want to populate with the code to update the main window.
Thats it, the dialog contains a table view which when the dialog is displayed automatically shows the database content. I want to trigger this stage automatically.
-
@AxelVienna Thats pretty much what I was thinking, except instead of adding additional code I want to trigger the same process that already occurs when the window is displayed.
-
@SPlatten said in Qt MainWindow how to close and reopen:
pobjProc->waitForFinished();
Why do you wait for process to finish? That blocks the event loop and is the reason why the cursor is not changed!
-
@jsulm I know that if I use HeidiSQL to import the 133Mb SQL file it doesn't complete instantly, when I do it in code the return is instant without waiting for the process which is occurring in the background to complete, that is why I put in the call to wait and want the wait cursor to be displayed so the use knows the process is doing something.
-
@SPlatten I really don't know how I should explain it better!
You set the cursor and then block Qt event loop, so you prevent Qt from actually changing the cursor or doing any other UI related stuff. This is very basic thing one needs to understand when using Qt.
Do not block event loop! So, do not wait for process to finish but use a slot connected to https://doc.qt.io/qt-5/qprocess.html#finished signal... -
@jsulm , ok, but as I said, it I remove the wait calls the process returns instantly and carries on in the background, no cursor is displayed, probably because there is no gap in the two cursors being displayed so is there a signal that I can connect to that I can use to restore the cursor when the process completes?
I'm going to try using the finished signal.
-
@SPlatten said in Qt MainWindow how to close and reopen:
no cursor is displayed
Move QApplication::restoreOverrideCursor() call to the slot connected to https://doc.qt.io/qt-5/qprocess.html#finished signal
-
I understand your motivation to wait for the import to finish. I propose to redesign your architecture a bit:
-
Table view Population
Encapsulate this in a separate method. You may want to add a bool parameter to tell it if the table view should or should not be cleared at the beginning. -
import
Run this in the background so it does not block the event loop as jsulm says. Define a signal with a parameter to tell the slot if the import was successful so it knows if it has to repopulate. -
wait cursor
Handle this at UI level, i.e. when the import button is pressed. Restore the cursor in the slot triggered by the import finished signal.
-
-
@AxelVienna , thank you, I will investigate.