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. Qt is event driven and yet there is no "start" signal?
Forum Updated to NodeBB v4.3 + New Features

Qt is event driven and yet there is no "start" signal?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 14.6k 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.
  • R Offline
    R Offline
    reactive
    wrote on last edited by
    #1

    Hi, I'm only a few weeks into Qt, but I've been programming for a long time.

    A "wtf" (I say that jokingly) I've stumbled on is that QCoreApplication has
    an "aboutToQuit" signal, but no matching "appStarting" signal.
    It really would be conceptually nice to have and might make the code nicer to look at and follow.
    Creating and showing widgets before exec() just seems "impure".

    Event driven programming is kind of like knocking a domino down and setting a rube
    goldberg machine into action. Most of the tutorials/examples feature trivial gui apps
    where some user action (like selecting a menu option) can put the app into motion -
    but what if a programmatic event needs to do this?

    With my (almost nonexistent) Qt knowledge all I can think to do would be something like:

    @
    int main(...){
    QCoreApplication app(...);
    // some QThread.start() where run(){ sleep for a few seconds, emit some signal }
    return app.exec();
    }
    @

    I know Qt started as a widget library, but in non-GUI apps where a signal isn't the result of
    a user interaction, a "start" signal would be great.

    [EDTI: code formatting, Volker]

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      You could use a Queued slot call to an object to achieve that:

      @
      QMetaObject::invokeMethod(pObj, SLOT(mySlot()), Qt::QueuedConnection, ...);
      @

      Or post yourselve an event with QCoreApplication::postEvent(...)

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • R Offline
        R Offline
        reactive
        wrote on last edited by
        #3

        My understanding is that exec() starts the event loop, right? So from your response I'm guessing it's ok to queue up events before it is started? That's good to know!

        I still say a "start" signal would make the API nicer. Josh Bloch gave a good Google Tech Talk once about designing good APIs and he speaks a bit about "symmetrical" APIs. Currently it's a little lopsided in this one respect. ;)

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          exec starts the event loop, that's correct. And quieued events are queued by the event loop so it should work. But I have never tried to queue an event before the event loop is running :-)

          Try it out :-)

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • B Offline
            B Offline
            baysmith
            wrote on last edited by
            #5

            QTimer::singleShot() with a timeout of 0 will send a signal when the event loop starts. You could use this on your own application class with an appStarting() signal.

            @
            #include <QtCore>
            #include <QtGui>

            class Application : public QApplication
            {
            Q_OBJECT
            public:
            Application(int &argc, char **argv)
            : QApplication(argc, argv)
            {
            QTimer::singleShot(0, this, SIGNAL(appStarting()));
            }

            signals:
            void appStarting();
            };

            int main(int argc, char *argv[])
            {
            Application app(argc, argv);

            QPushButton *button = new QPushButton("Test");
            QObject::connect(&app, SIGNAL(appStarting()), button, SLOT(show()));
            QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()));
            
            return app.exec();
            

            }

            #include "main.moc"
            @

            Nokia Certified Qt Specialist.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              [quote author="Gerolf" date="1296330631"]exec starts the event loop, that's correct. And quieued events are queued by the event loop so it should work. But I have never tried to queue an event before the event loop is running :-)

              Try it out :-)[/quote]

              Calling show() on the widget queues the first events before QApplication's exec starts (IMHO - I did not check the sources!) - so you're doing this all the time with the samples :-)

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • V Offline
                V Offline
                vidar
                wrote on last edited by
                #7

                I just wanted to push this thread, as I got the same "problem". I solved it by checking the receipt of a signal by polling via a timer.
                It would be great, if the exec() function could emit a "event loop started" signal. Maybe a Qt developer will read this. :-)

                1 Reply Last reply
                1
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  The single shot timer mentioned by Bradley is a good solution for this.

                  If you want to attract the devs' attentions, please report your request in the "public bugtracker":https://bugreports.qt.nokia.com. This forum is not monitored regularly by the trolls.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    vidar
                    wrote on last edited by
                    #9

                    The single shot timer solution did not work in my scenario (or I did something wrong ;). Thanks, I think I will post a feature request.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #10

                      Another option woulkd be to implement the stuff and file a merge request on www.gitorious.org for Qt sources.

                      Nokia Certified Qt Specialist.
                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                      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