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
Forum Updated to NodeBB v4.3 + New Features

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 1 Watching
  • 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 13 Nov 2018, 12:04 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

    J J 2 Replies Last reply 13 Nov 2018, 12:24
    0
    • S saurabh162
      13 Nov 2018, 12:04

      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 Offline
      J Offline
      JonB
      wrote on 13 Nov 2018, 12:24 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
      • D Offline
        D Offline
        dheerendra
        Qt Champions 2022
        wrote on 13 Nov 2018, 14:37 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
          13 Nov 2018, 12:04

          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 Online
          J Online
          J.Hilk
          Moderators
          wrote on 13 Nov 2018, 14:50 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 13 Nov 2018, 15:51 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
            • D Offline
              D Offline
              dheerendra
              Qt Champions 2022
              wrote on 13 Nov 2018, 16:57 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 14 Nov 2018, 08:29 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
                • D Offline
                  D Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on 14 Nov 2018, 10:34 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 14 Nov 2018, 12:28 last edited by
                    #9

                    Hello @dheerendra,

                    I implemented the solution as you suggested.

                    Thank you :)

                    1 Reply Last reply
                    0

                    1/9

                    13 Nov 2018, 12:04

                    • Login

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