Results 1 to 6 of 6

Thread: signal/slot different approach

  1. #1
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default signal/slot different approach

    what's the difference between the two declaration below
    Qt Code:
    1. connect(object1,SIGNAL(clicked()),SIGNAL(mySignal()));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. connect(object1,SIGNAL(clicked()),this,SLOT(mySlot()));
    2. void mySlot()
    3. {
    4. emit mySignal();
    5. }
    To copy to clipboard, switch view to plain text mode 
    which is preferred and why..

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: signal/slot different approach

    Quote Originally Posted by salmanmanekia View Post
    what's the difference between the two declaration below
    Qt Code:
    1. connect(object1,SIGNAL(clicked()),SIGNAL(mySignal()));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. connect(object1,SIGNAL(clicked()),this,SLOT(mySlot()));
    2. void mySlot()
    3. {
    4. emit mySignal();
    5. }
    To copy to clipboard, switch view to plain text mode 
    which is preferred and why..
    the third parameter (this) is the receiver, if reciver isn't specify than an object, in which connect is used, is used by default.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: signal/slot different approach

    Quote Originally Posted by salmanmanekia View Post
    which is preferred and why..
    First one is cleaner and will be one invokation faster than the other.

  4. #4
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: signal/slot different approach

    First one is cleaner and will be one invokation faster than the other.
    can you please elobrate on 'invokation'

  5. #5
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: signal/slot different approach

    Quote Originally Posted by salmanmanekia View Post
    what's the difference between the two declaration below
    Qt Code:
    1. connect(object1,SIGNAL(clicked()),SIGNAL(mySignal()));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. connect(object1,SIGNAL(clicked()),this,SLOT(mySlot()));
    2. void mySlot()
    3. {
    4. emit mySignal();
    5. }
    To copy to clipboard, switch view to plain text mode 
    which is preferred and why..
    The difference is that the first connect connects a signal to another signal (with "this" as a receiver). It means that mySignal will be emited as soon as clicked signal emits. I think usually further action would be to connect mySignal to a slot (just like the 2nd connect statement).
    I'm a rebel in the S.D.G.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: signal/slot different approach

    Quote Originally Posted by salmanmanekia View Post
    can you please elobrate on 'invokation'
    Function call. Signal emition is a function call and so is slot activation, so if you don't activate the slot and instead emit the signal directly, you'll have one function call less (which is probably saving about up to 20 cpu cycles, so it's completely irrelevant).

Similar Threads

  1. Replies: 8
    Last Post: 25th February 2008, 17:00
  2. Passing a pointer in Signal/Slot Connection
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2007, 19:04
  3. What argument types for Signal/Slot?
    By DPinLV in forum Qt Programming
    Replies: 5
    Last Post: 2nd August 2006, 19:08
  4. What if not Signal/Slot
    By munna in forum General Discussion
    Replies: 2
    Last Post: 26th May 2006, 06:48
  5. Replies: 4
    Last Post: 23rd January 2006, 16:51

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.