Results 1 to 5 of 5

Thread: sending signals between two different instances of two different classes?

  1. #1
    Join Date
    May 2012
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default sending signals between two different instances of two different classes?

    I love the way signals and slots works compared to callback functions, but I'm having a problem that I might have solved in an awkward way.
    I have two classes, class A and B. I create an instance of each, and then somewhere in class A, I have to emit a signal to a slot in class B.

    But how do I get that instance of B at runtime?


    What I did (which is the awkward part):

    Qt Code:
    1. A foo;
    2. B bar;
    3.  
    4. QObject::connect(&foo, SIGNAL(someSignal()), &bar, SLOT(someSlot()));
    To copy to clipboard, switch view to plain text mode 

    I've used signals and slots once before (about a week ago), so I know how they work (I think), but there is some other way to do this, I hope..is there?

    Thanks!

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

    Default Re: sending signals between two different instances of two different classes?

    But how do I get that instance of B at runtime/
    Like you have or by keeping pointers to the objects. Once connected they stay connected.

    I am not sure what you are expecting.

  3. #3
    Join Date
    May 2012
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: sending signals between two different instances of two different classes?

    so then it's not bad practice to have connections in main()? If not, then I guess my problem is solved.
    Thanks ChrisW67!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: sending signals between two different instances of two different classes?

    Quote Originally Posted by dsab123 View Post
    so then it's not bad practice to have connections in main()?
    Quite the opposite. It's best to have connect statements between objects A and B outside of their definitions. This provides so called "loose coupling" of objects.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: sending signals between two different instances of two different classes?

    Quote Originally Posted by dsab123 View Post
    so then it's not bad practice to have connections in main()? If not, then I guess my problem is solved.
    There's nothing in my response and nothing in your question regarding main() or where you should/should not connect objects.

    As Wysota says, you generally connect objects from somewhere outside the objects. If main() is the logical place to do that then do it there.

Similar Threads

  1. Sending signals from QPushButton
    By Cucus in forum Newbie
    Replies: 3
    Last Post: 19th July 2011, 04:06
  2. Replies: 1
    Last Post: 9th April 2011, 21:07
  3. Sending OS I/O interrupt signals!
    By nofortee in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2010, 15:42
  4. Sending signals from one module to another
    By Dubbie in forum Newbie
    Replies: 5
    Last Post: 6th July 2010, 02:52
  5. sending signals to ancestors
    By magland in forum Qt Programming
    Replies: 3
    Last Post: 6th October 2007, 11:51

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.