Results 1 to 5 of 5

Thread: How to emit a signal passed to a function

  1. #1
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to emit a signal passed to a function

    I am passing a signal to a method using the SIGNAL macro, and at some point I need to emit that signal.
    Something like the below:

    Qt Code:
    1. void Network::get(const QNetworkRequest &request, const char *signal)
    2. {
    3. QNetworkReply *reply = m_manager->get(request);
    4. m_requests.insert(reply, signal);
    5. }
    To copy to clipboard, switch view to plain text mode 

    the Network class encapsulates QNetworkAccessManager, so in the method above m_manager is QNetworkAccessManager.
    The Network class has the following signal/slot connection:
    Qt Code:
    1. connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResponse(QNetworkReply*)));
    To copy to clipboard, switch view to plain text mode 
    the onResponse SLOT is:
    Qt Code:
    1. void Network::onResponse(QNetworkReply *reply)
    2. {
    3. const char *signal = m_requests.take(reply);
    4. // emit signal somehow
    5. qDebug() << "and the signal is: " << signal;
    6. }
    To copy to clipboard, switch view to plain text mode 
    m_request is: QHash<QNetworkReply *, const char *> m_requests;

    The idea is that various other classes of the application can use the Network class to make http requests. Each of those classes calls void Network::get(const QNetworkRequest &request, const char *signal) passing its own signal that should be emitted once the request is finished.
    I prototype a small concept of the application using PySide and this mechanism of passing the signal worked perfectly.
    But in the C++ version the return of the SIGNAL macro is a const char *, is there a way i can turn the const char * back to an emittable signal?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,688
    Thanks
    13
    Thanked 1,598 Times in 1,526 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to emit a signal passed to a function

    Why are you trying to reinvent this wheel? Each caller of QNetworkAccessManager::get() is given a QNetworkReply object. They can connect that object's QNetworkReply::finished() signal to any slot they want executed, or to any signal they want emitted, independent of any other reply object.

  3. #3
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to emit a signal passed to a function

    I am not trying to reinvent the wheel, I just did not realized that QNetworkReply is also emitting the finished signal.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to emit a signal passed to a function

    In case you ever need this in a different context, don't pass a signal but a slot.

    If you have receiver object and a slot name you can easily use QMetaObject::invokeMethod() to call that slot.
    Even with arguments.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: How to emit a signal passed to a function

    If I'm not mistaken a signal is just a member function, so you should able to store pointers to the signals instead of their const char* representation.
    See this for information: http://stackoverflow.com/a/15176860/4206247

    You also need the instance of the QObject that is emitting it.

Similar Threads

  1. how to emit signal in a static function ?
    By cxl2253 in forum Qt Programming
    Replies: 32
    Last Post: 7th July 2016, 22:36
  2. How to emit signal from static callback function
    By blizniak83 in forum Qt Programming
    Replies: 3
    Last Post: 14th April 2012, 09:56
  3. Replies: 4
    Last Post: 4th January 2012, 23:43
  4. Replies: 4
    Last Post: 20th January 2011, 02:03
  5. Emit signal from const function
    By waynew in forum Qt Programming
    Replies: 9
    Last Post: 22nd August 2010, 14:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.