Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Running intialization code in MainWindow constructor and displaying message to user

Running intialization code in MainWindow constructor and displaying message to user

Scheduled Pinned Locked Moved Solved Mobile and Embedded
9 Posts 4 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saurabh162
    wrote on last edited by
    #1

    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

    JonBJ J.HilkJ 2 Replies Last reply
    0
    • S saurabh162

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @saurabh162
      Yes, the main window will not display at all till you have called show()!

      • 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.

      1 Reply Last reply
      3
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by dheerendra
        #3

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • S saurabh162

          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

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @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 in IntializationCode. So this example should crash due to invalid pointers.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            saurabh162
            wrote on last edited by saurabh162
            #5

            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.

            1. Intialize a single shot timer in mainwindow constructor whose slot will open a model Dialog Box and Intialization code.
            2. 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 :)

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              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.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              3
              • S Offline
                S Offline
                saurabh162
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

                  Good to hear. If the issue is resolved move the case to "Solved" state. Enjoy the Qt programming.

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    saurabh162
                    wrote on last edited by
                    #9

                    Hello @dheerendra,

                    I implemented the solution as you suggested.

                    Thank you :)

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved