Results 1 to 17 of 17

Thread: replacing signals and slots with callback functions

  1. #1
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default replacing signals and slots with callback functions

    Hi

    I wanted to replace the signals and slots in my application to callback functions.
    example QObject::connect(c,SIGNAL(sendSignal2(void *)), c ,SLOT(Some_Function(void *)));
    how to do the same functionality with callback... how to do this?

  2. #2

    Default Re: replacing signals and slots with callback functions

    Why would you want to replace the signal/slot mechanism with callbacks?

  3. #3
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: replacing signals and slots with callback functions

    just wanted to check wether callbacks are faster than signals and slots.. as in my application they are taking much of a time..

    is callback is faster than signals/slots? anybody have tested it? i wanted to test the same.. how to replace the code please help
    Last edited by meena; 20th August 2010 at 11:31.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: replacing signals and slots with callback functions

    . as in my application they are taking much of a time..
    Dont think they can be slow.. how much time are they taking ??

    If you want to use callbacks,, it will be your design. I dont think Qt does that for you.

  5. #5
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: replacing signals and slots with callback functions

    They are taking around 550 mili seconds ... i understand that qt does not have anything for callbacks, but i wanted a help to replace those above statements.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: replacing signals and slots with callback functions

    In a way, signals/slots are a callback (direct connection), unless its not possible, in which case they use events (queued connection).

    If you want to use callbacks you will have to implement the functionality yourself (eg, pass the functinos pointers to your relevent objects)

  7. #7

    Default Re: replacing signals and slots with callback functions

    The real reason Qt got off the ground in the first place is because the whole callback issue got ugly in other frameworks.
    By reintroducing them you'd destroy the thing Qt is best at.

    Signals and slots are marginally slower than callbacks, but they are typesafe.

  8. #8
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: replacing signals and slots with callback functions

    500 ms = half a second? That cannot be true, please measure again. The emit statement is just a function call, so I guess you are timing your own function.

    As someone as pointed out, when you connect() the signals you can pass "Qt::QueuedConnection" to speed up the emit (the function will be called later by the event loop)

    Connect: http://doc.trolltech.com/4.6/qobject.html#connect
    Qt::QueuedConnection: http://doc.trolltech.com/4.6/qt.html...ctionType-enum

  9. #9
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: replacing signals and slots with callback functions

    The difference in execution speed between signals and slots is measured in a handful of milliseconds at most. In other words, it will be unnoticeable in all but the most pathological programs.

    Rather than thrash around trying to replace an elegant software system with one that is error prone, buggy and semantically ugly - on a good day - and which is almost certainly not causing a significant slowdown - try running your application through a profiler to determine where it is spending it's time, and why.

  10. #10
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: replacing signals and slots with callback functions

    thanks everybody for replying, can u suggest/give some profiling tool and how to use them
    Last edited by meena; 21st August 2010 at 09:11.

  11. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: replacing signals and slots with callback functions

    That depends on your processor, some only work on Intel, some only on AMD, and some on both.

    Intel used to provide a free one, but I think it's now commercial, called VTune.
    AMD provide CodeAnalyst, which I think is still free.

    then there's lot of third party ones which cost varying amounts.

    If your happy with CLI, Visual studio comes with a free one, as does GCC.

  12. #12
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: replacing signals and slots with callback functions

    I am working on Linux, (32 bit fedora), My processor is AMD, I need to get the profiling data for qt project which i will be using for ARM 9 (I have arm cross compiler/arm tool chain , 32 bit), anything specific for me?

  13. #13
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: replacing signals and slots with callback functions

    Try valgrind or oprofile etc.
    If you're really adventurous, systemtap.

    These tools all come with a high learning curve though.

  14. #14
    Join Date
    Dec 2007
    Posts
    27
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: replacing signals and slots with callback functions

    The Qt documentation says the following to the run time difference between callbacks and signal/slots:

    "Compared to callbacks, signals and slots are slightly slower because of the increased flexibility they provide, although the difference for real applications is insignificant. In general, emitting a signal that is connected to some slots, is approximately ten times slower than calling the receivers directly, with non-virtual function calls. This is the overhead required to locate the connection object, to safely iterate over all connections (i.e. checking that subsequent receivers have not been destroyed during the emission), and to marshall any parameters in a generic fashion. While ten non-virtual function calls may sound like a lot, it's much less overhead than any new or delete operation, for example. As soon as you perform a string, vector or list operation that behind the scene requires new or delete, the signals and slots overhead is only responsible for a very small proportion of the complete function call costs.

    The same is true whenever you do a system call in a slot; or indirectly call more than ten functions. On an i586-500, you can emit around 2,000,000 signals per second connected to one receiver, or around 1,200,000 per second connected to two receivers. The simplicity and flexibility of the signals and slots mechanism is well worth the overhead, which your users won't even notice."

    http://doc.qt.nokia.com/4.6/signalsandslots.html

  15. #15
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: replacing signals and slots with callback functions

    Quote Originally Posted by meena View Post
    I am working on Linux, (32 bit fedora), My processor is AMD, I need to get the profiling data for qt project which i will be using for ARM 9 (I have arm cross compiler/arm tool chain , 32 bit), anything specific for me?
    gprof - http://www.cs.utah.edu/dept/old/texi...prof.html#SEC2

  16. #16
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: replacing signals and slots with callback functions

    Thanks all of you, i will try to do profiling.

  17. #17
    Join Date
    May 2010
    Location
    Bangalore, India.
    Posts
    28
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: replacing signals and slots with callback functions

    gprof is working fine with qt projects, but the project where there are external libraries (for sensors which are connected to the board) included in the project there gmon.out is not getting created (Those libraries are compatible for arm, and i have arm tool chain, have arm-linux-gprof installed), in the project file i have included the lines
    CONFIG += DEBUG
    QMAKE_CXXFLAGS_DEBUG += -pg
    QMAKE_LFLAGS_DEBUG += -pg
    why gmon.out is not getting created..? are there anything extra i need to do?

Similar Threads

  1. Cannot call OpenCV 2.0 functions inside Qt slots
    By Asfer in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2010, 11:48
  2. Regards CallBack Functions
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2009, 16:16
  3. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  4. regarding signals/slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 09:32
  5. emiting signals from const member functions !?
    By sunil.thaha in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2006, 11:29

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.