Results 1 to 10 of 10

Thread: connect slot signlas with class references?

  1. #1
    Join Date
    Jun 2010
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default connect slot signlas with class references?

    Hi,

    How do I pass the signals reference to the slot function? Take this snippet as an example:

    Qt Code:
    1. connect(button, SIGNAL( mySignal(QString) ), this, SLOT( mySlot(QString) ));
    2.  
    3. void MainWindow::mySlot(const QString &title)
    4. {
    5. // how can I get the button reference in here?
    6. }
    To copy to clipboard, switch view to plain text mode 

    I tried to pass "this" in mySignal but it doesn't seem to work...

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: connect slot signlas with class references?

    What problem do you want to solve?

    I don't understand why do you need the button in there, the button emitted a signal with a QString parameter, so in there you use that, or recode that signal to transmit what you need.

  3. #3
    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: connect slot signlas with class references?

    There are two solutions:

    1. Use QSignalMapper
    2. Use the sender() function

  4. The following user says thank you to tbscope for this useful post:

    dennis (30th June 2010)

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

    Default Re: connect slot signlas with class references?

    If you think you need to know what object sent the signal, it's usually a sign that you need to rethink your design. You're coupling the recipient to the sender, requiring one to have explicit knowledge of the other. Normally, this is not a good thing; it violates the encapsulation principles of OO design, and can potentially create maintenance headaches in the future, when the internal details of an object change.

    As noted above, there are ways to make this work, but take a moment and consider whether you really need to do this.

  6. The following user says thank you to SixDegrees for this useful post:

    Zlatomir (30th June 2010)

  7. #5
    Join Date
    Jun 2010
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect slot signlas with class references?

    Quote Originally Posted by tbscope View Post
    There are two solutions:

    1. Use QSignalMapper
    2. Use the sender() function
    Thanks sender() worked, I then used dynamic_cast<buttonClass*>(sender()) to get the button reference, whats the difference between dynamic_cast and any other cast?

    Quote Originally Posted by SixDegrees View Post
    If you think you need to know what object sent the signal, it's usually a sign that you need to rethink your design. You're coupling the recipient to the sender, requiring one to have explicit knowledge of the other. Normally, this is not a good thing; it violates the encapsulation principles of OO design, and can potentially create maintenance headaches in the future, when the internal details of an object change.
    Doesn't that depends on what your trying to do? I mean if your placing a dynamic number of items in a tab widget you probably want to know which of those item that actually sent you a signal, from my experience with other languages you pass the event to the event listener (slot) and then fetch the class reference from there. But in Qt (not sure if its the same in regular c++) you don't pass the event.

  8. #6
    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: connect slot signlas with class references?

    Quote Originally Posted by dennis View Post
    Thanks sender() worked, I then used dynamic_cast<buttonClass*>(sender()) to get the button reference, whats the difference between dynamic_cast and any other cast?
    First, try looking it up in a c++ book or on google.

    Suppose you have a cube and a circular hole. You can't just push the cube through the round hole.
    When you use a dynamic cast, you take a sledge hammer and whack the cube through the round hole. The result is that the cube went through the hole and it now resembles somewhat like a cylinder.
    A static cast first looks at what both things are, a cube, and a round hole... hmmm, this isn't going to work, thus it will do nothing unless both objects are compatible

    After that, you get in the weird quantum world with some special casts where one object can be in two states at once ;-)

    In your example, I personally think it would not be a good idea to use a dynamic cast. I would use a static cast, and see if the object exists at all before proceeding. Hence, this is why most people try to avoid using sender() at all.

    You could use a subclass, and create your custom signals that pass a pointer to the object too (or something else to identify a specific object). See also QSignalMapper

  9. The following user says thank you to tbscope for this useful post:

    dennis (30th June 2010)

  10. #7
    Join Date
    Jun 2010
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: connect slot signlas with class references?

    Thanks for taking you time to explain it to a newbie like me =)

  11. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: connect slot signlas with class references?

    @tbscope: You reversed the casts

    static_cast is similar to the C style cast and can be applied to just about anything. static_cast would be used when you are certain of the types in question.

    dynamic_cast can only be used with pointers and references. On failure to cast, a null pointer is returned. dynamic_cast is generally used when resolving pointers to classes used in inheritance where you want to make sure the pointer you are casting is of the expected type.

  12. #9
    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: connect slot signlas with class references?

    Ohh dear :-)

    I should practice what I preach, read a c++ book.
    I blame the warm weather here.

  13. #10
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: connect slot signlas with class references?

    @tbscope: hahahaha, nice

    And now seriously, really nice comparison with the cube and a round hole! I think you should edit the post, because the comparison is awesome (too bad we don't have thumbs-up smiley)

Similar Threads

  1. connect a qpushbutton a slot
    By Lycus HackerEmo in forum Newbie
    Replies: 13
    Last Post: 29th March 2010, 09:14
  2. Class value members & references
    By agnus in forum General Programming
    Replies: 3
    Last Post: 6th December 2009, 14:18
  3. Replies: 12
    Last Post: 18th September 2008, 15:04
  4. Qt Designer & Qt4, connect to my own slot.
    By geitosten in forum Newbie
    Replies: 2
    Last Post: 17th February 2007, 19:22
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

Tags for this Thread

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.