Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Looking to call a function withour user intervention
Forum Updated to NodeBB v4.3 + New Features

Looking to call a function withour user intervention

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 2.2k 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.
  • bart.hollisB bart.hollis

    Complete noob, learning as I go, creating an QWidget desktop application by trial and error. My application (program?) is a databased application and will be using SQLite. As I do not want to have to include a data file with the application, I want to create the db file and the necessary tables programmatically. I also want to give the user opportunity to populate the tables with sample data.

    I want the program to start, at the startup page, check for the existence if the database, check for the existence of the tables, check to see if the tables are empty and if so, give the user the opportunity to insert the sample data.

    Now, I have written a function to do all this, but! I can't get the function to run without having the user click on something. I've move the call to the function call around in different places and doing so have created some awful results! Including having a copy of the program running that can't be accessed until the user logs out!

    So, after all this my question is: What can I use to call the function after the program has loaded and settled down waiting for user input but without user input?

    Perhaps my question is too long, but I wanted to avoid try to help but have to ask me all the questions I just answered.

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

    @bart.hollis
    You can write your code to execute just before your call to QApplication::exec() (or whatever runs your main event loop).

    During that time you can certainly do *non-*interactive things you mention, like looking at the database etc.

    When you come to:

    I also want to give the user opportunity to populate the tables with sample data.

    This is user interaction. You can still put up a QDialog (plus QMessageBox etc.) to ask user a question and act on it, before you have got as far as entering the maim event loop.

    1 Reply Last reply
    1
    • bart.hollisB Offline
      bart.hollisB Offline
      bart.hollis
      wrote on last edited by
      #3

      Thank you for your reply.
      Ah yes. I had tried that but discarded it as a QMessageBox poping up outside of, or before the main window appears just looks wrong to me. I would like the UI to settle before doing this.

      JonBJ W 2 Replies Last reply
      0
      • bart.hollisB bart.hollis

        Thank you for your reply.
        Ah yes. I had tried that but discarded it as a QMessageBox poping up outside of, or before the main window appears just looks wrong to me. I would like the UI to settle before doing this.

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

        @bart.hollis
        If that's what you want, you could set off a one-shot QTimer (just prior to QApplication::exec()) to bring up your UI after a fraction of a second.

        EDIT: Or, maybe QEvent::Show in QMainWindow::event(QEvent *event) (or QWidget::showEvent) will be raised when your main window has been shown.

        There are all sorts of suggestions in https://stackoverflow.com/questions/14356121/how-to-call-function-after-window-is-shown

        1 Reply Last reply
        2
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          Hi,

          Some something like:

          1. "show widgets"
          2. Start method while widgets are visible

          ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          bart.hollisB 1 Reply Last reply
          0
          • bart.hollisB bart.hollis

            Thank you for your reply.
            Ah yes. I had tried that but discarded it as a QMessageBox poping up outside of, or before the main window appears just looks wrong to me. I would like the UI to settle before doing this.

            W Offline
            W Offline
            wrosecrans
            wrote on last edited by
            #6

            @bart.hollis What's making the main window appear? If you want to be able to tell the main window to either show data or prompt the user about populating it with sample data as soon as it opens, that might be a good place to put the check for the data.

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Some something like:

              1. "show widgets"
              2. Start method while widgets are visible

              ?

              bart.hollisB Offline
              bart.hollisB Offline
              bart.hollis
              wrote on last edited by
              #7

              @SGaist said in Looking to call a function withour user intervention:

              Hi,

              Some something like:

              1. "show widgets"
              2. Start method while widgets are visible

              ?

              Yup! That's what I want to do, but can't seem to find something, or one of the widgets that will trigger the function. Wish I could get into the actual loop! :)

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                Use a QTimer::singleShot with a value of 0 for the delay, then you'll have your GUI running and the method called right after the first round of the event loop.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                bart.hollisB 1 Reply Last reply
                2
                • SGaistS SGaist

                  Use a QTimer::singleShot with a value of 0 for the delay, then you'll have your GUI running and the method called right after the first round of the event loop.

                  bart.hollisB Offline
                  bart.hollisB Offline
                  bart.hollis
                  wrote on last edited by
                  #9

                  @SGaist said in Looking to call a function withour user intervention:

                  Use a QTimer::singleShot with a value of 0 for the delay, then you'll have your GUI running and the method called right after the first round of the event loop.

                  Could you please give me more detail as how to implement this?

                  JonBJ 1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by SGaist
                    #10

                    Same as usual except that you use 0 as timeout value and you call that as the last thing in your constructor.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    bart.hollisB 1 Reply Last reply
                    2
                    • bart.hollisB bart.hollis

                      @SGaist said in Looking to call a function withour user intervention:

                      Use a QTimer::singleShot with a value of 0 for the delay, then you'll have your GUI running and the method called right after the first round of the event loop.

                      Could you please give me more detail as how to implement this?

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

                      @bart.hollis
                      I suggested you look at https://stackoverflow.com/a/38749911/489865 or which does actually give code for using QTimer::singleShot.

                      1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Same as usual except that you use 0 as timeout value and you call that as the last thing in your constructor.

                        bart.hollisB Offline
                        bart.hollisB Offline
                        bart.hollis
                        wrote on last edited by
                        #12

                        @SGaist said in Looking to call a function withour user intervention:

                        Same as usual except that you use 0 as timeout value and you call that as the last thing in your constructor.

                        That would be the MainWindow constructor?

                        Then, do I understand correctly that I use the slot mentioned to trigger my function?

                        From Docs: QTimer::singleShot(200, this, SLOT(updateCaption()));

                        1 Reply Last reply
                        0
                        • bart.hollisB Offline
                          bart.hollisB Offline
                          bart.hollis
                          wrote on last edited by
                          #13

                          SGaist
                          Your suggestion works just as I want! Now, I just need to understand WHY! :)

                          1 Reply Last reply
                          0
                          • bart.hollisB Offline
                            bart.hollisB Offline
                            bart.hollis
                            wrote on last edited by bart.hollis
                            #14

                            Now, I need to mark this thread as solved. I know how to do that!
                            But I should click something that gives points to reputation. I know I saw that somewhere! (mutter, mutter, mutter) I hate when I don't know what I'm doing!

                            Anyway, Thanks SGaist! Very Much!

                            aha_1980A 1 Reply Last reply
                            0
                            • bart.hollisB bart.hollis

                              Now, I need to mark this thread as solved. I know how to do that!
                              But I should click something that gives points to reputation. I know I saw that somewhere! (mutter, mutter, mutter) I hate when I don't know what I'm doing!

                              Anyway, Thanks SGaist! Very Much!

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              @bart.hollis

                              You can upvote a post with the small up arrow right below each post. The down arrow is for downvoting (and should be used in rare cases). The number in between the both shows the current voting for a post and are therefore a sign for it's quality.

                              Regards

                              Qt has to stay free or it will die.

                              1 Reply Last reply
                              0
                              • bart.hollisB Offline
                                bart.hollisB Offline
                                bart.hollis
                                wrote on last edited by
                                #16

                                AHA! (Not trying to steal your handle!) Thanks!

                                aha_1980A 1 Reply Last reply
                                0
                                • bart.hollisB bart.hollis

                                  AHA! (Not trying to steal your handle!) Thanks!

                                  aha_1980A Offline
                                  aha_1980A Offline
                                  aha_1980
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #17

                                  @bart.hollis You're welcome :)

                                  Qt has to stay free or it will die.

                                  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