Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. How to defer emitting signal?
QtWS25 Last Chance

How to defer emitting signal?

Scheduled Pinned Locked Moved Qt WebKit
13 Posts 5 Posters 19.1k 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.
  • Q Offline
    Q Offline
    qiezi
    wrote on last edited by
    #1

    I want to custom QNetworkAccessManager to load resource from memory, and I implemented a QMyReply:

    @
    QMyReply::QMyReply(...)
    {
    // ... init some data
    emit readyRead();
    emit finished();
    }
    @

    It can't work, because no slot connected to readyRead/finished signal when emit signal.

    My solution is:

    @
    QMyReply::QMyReply(...)
    {
    // ... init some data
    QTimer::singleShot(0, this, SLOT(deferEmitSignals()));
    }

    void QMyReply::deferEmitSignals()
    {
    emit readyRead();
    emit finished();
    }
    @

    It's ugly.

    What is the better way that defer emitting signal?

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

      Hi quizi

      it does not make sense to emit signas in the constructor.
      If you really need similar stuff; i see no other chance than to use a single shot timer.

      But you should ensure, that the connects can be done then.

      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
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        It's what is extensively used inside QNAM as of now, so I think it's perfectly acceptable.

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mario
          wrote on last edited by
          #4

          You can also use queued connections when connecting the signal. Probably you want to do have this in a private wrapper class to be sure that the connections are queued.

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

            Queued connects do not help here, as he wants to emit the signals in his constructor. In the constructor, no connects to the signals ob the objects exist! The queued connect could only replace the single shot timer, which is no difference...

            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
            • M Offline
              M Offline
              mario
              wrote on last edited by
              #6

              I just wanted to tell him that he can use queued connection to accomplish the same thing as timers.

              For example:
              @
              connect(sender, SIGNAL(finished()), SIGNAL(finished()), QueuedConnection);
              @

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

                But he implements sender. And in the constructor of sender he wants to emit the signal. So the connection is too late.

                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
                • Q Offline
                  Q Offline
                  qiezi
                  wrote on last edited by
                  #8

                  Thanks all.

                  I don't know where QNetworkAccessMnager::createRequest is called, I guess the code is:

                  @
                  QNetworkReply* reply = networkAccessManager->createRequest();
                  connect(reply, SIGNAL(finished()), someObj, SLOG(someSlot()));
                  // reply->someMethod();
                  @

                  Is there a method call like reply->someMethod() after connect? I want to emit signal in this method.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mario
                    wrote on last edited by
                    #9

                    Gerolf: Something like this should work

                    @
                    class Dummy : public QObject
                    {
                    ...
                    public:
                    void emitFinished();
                    signal:
                    void finished();
                    }

                    QMyReply::QMyReply(...)
                    {
                    Dummy *dummy = new QObject(this);
                    connect(dummy, SIGNAL(finished()), SIGNAL(finished()), QueuedConnection);

                    dummy->emitFinished();
                    

                    }
                    @

                    I didn't say it would be easier/cleaner, only that he could also use QueuedConnection :)

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

                      Hi mario,

                      but it makes the whole complex more complex, requires more memory and QObject event does not have a finished signal. So he needs to implement some object with that signal.
                      Then it would be easier to make a queued call to one self:

                      "QMetaObject::invokeMethod": http://doc.qt.nokia.com/latest/qmetaobject.html#invokeMethod with some parameters for queued function call or use an internal own slot with queued connection (which a single shot timer also does).

                      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
                      1
                      • M Offline
                        M Offline
                        mario
                        wrote on last edited by
                        #11

                        Ah, sweet. I totally forgot about invokeMethod. The funny thing is that I recently considered to use invokeMethod myself to replace a switch/case statement by simply doing invokeMethod for a bunch of signals :)

                        But I agree, seems that timer is the easiest approach in this case.

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          [quote author="qiezi" date="1301266313"]Thanks all.
                          Is there a method call like reply->someMethod() after connect? I want to emit signal in this method.[/quote]

                          There is, actually. Take a look at QObject::connectNotify() and QObject::disconnectNotify().

                          1 Reply Last reply
                          0
                          • Q Offline
                            Q Offline
                            qiezi
                            wrote on last edited by
                            #13

                            Great! Thank you.

                            Thanks everyone.

                            [quote author="Gerolf" date="1301294329"]Hi mario,

                            but it makes the whole complex more complex, requires more memory and QObject event does not have a finished signal. So he needs to implement some object with that signal.
                            Then it would be easier to make a queued call to one self:

                            "QMetaObject::invokeMethod": http://doc.qt.nokia.com/latest/qmetaobject.html#invokeMethod with some parameters for queued function call or use an internal own slot with queued connection (which a single shot timer also does).
                            [/quote]

                            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