Solved Running intialization code in MainWindow constructor and displaying message to user
-
Dear developers,
Before starting my QT GUI I want to run some intialisation code(which also includes delays implemented using single shot timer) for hardware from my QT application which takes about 3 minutes to complete.
Additionally while the intialisation code for hardware is running, I want to show message to user that he has to wait for 3 minutes before
he can use GUI.I would be grateful to you if you please tell me, what is correct way to implement it ?
Right now I am calling intialization code in MainWindow constructor as given below to acheive above mentioned objective.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->mainLabel->setText("Wait for 3 minutes as Intialization code is ruuning"); IntializationCode(); }
But problem, I am facing is that the Message on "ui->mainLabel" is displayed once IntializationCode(); has finished executing and not before
IntializationCode(); has started executing.One reason for above problem can be that w.show(); function is called after constructor of MainWindow in main() function. So please inform me how can I achieve above mentioned objective.
Thank you very much
Kind Regards
Saurabh
-
@saurabh162
Yes, the main window will not display at all till you have calledshow()
!- You either have to allow it show, and perhaps do your
IntializationCode();
in the background/on a timer. - Or possibly change to using a modal/modeless dialog instead of the main window.
You'll have to verify that at least a single-shot timer will work in either of these situations, when you haven't entered the Qt event loop yet, I don't know.
- You either have to allow it show, and perhaps do your
-
In addition to what @JonB said, please note that show() and exec() function have to be called before you start displaying. Alternatively you can use the QMessageBox to show the some information to the user.
-
@saurabh162 said in Running intialization code in MainWindow constructor and displaying message to user:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->mainLabel->setText("Wait for 3 minutes as Intialization code is ruuning");
IntializationCode();
}to add my 2 cents,
ui->mainLabel will remain invalid until you call
ui->setupUi(this);
which i presume you do inIntializationCode
. So this example should crash due to invalid pointers. -
Re: Running intialization code in MainWindow constructor and displaying message to user
Hello @JonB, @dheerendera, @J-Hilk
Thank you very much for your fast reply
Actually I planning to take following steps to achieve my goal.
- Intialize a single shot timer in mainwindow constructor whose slot will open a model Dialog Box and Intialization code.
- In called model Dialog Box i will show message to user about what is going on in hardware and how much time will it still take to get ready for use.
Please let me know if I am doing anything wrong.
@J-Hilk I have called "ui->setupUi(this);" in my MainWindow constructor as given below.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->mainLabel->setText("Wait for 3 minutes as Intialization code is ruuning"); IntializationCode(); }
Thank you :)
-
Do the initialisation of code in the timer slot. Show the ProgressBar on the UI. Once the init is complete in the timer slot, close the progressBar.
-
Hello @dheerendra
Thank you very much for your update
I think your solution is neat and easy to implement than the one I had. I will implement the solution suggested by you.
Kind regards
Saurabh Jain -
Good to hear. If the issue is resolved move the case to "Solved" state. Enjoy the Qt programming.
-