Results 1 to 2 of 2

Thread: Connect Signal/Slot to any instance of another class

  1. #1
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Connect Signal/Slot to any instance of another class

    There's probably an answer for this somewhere out there, but its a difficult search to word that doesn't return 100 million results of basic signal/slot questions.

    I have a single instance of a class that owns a countdown timer, when the timer reaches 0, I would like it to emit a signal that can be received by any instance of another class without having to hard-code each instance. This obviously can be easily performed by writing out the code for each instance, such as:

    Qt Code:
    1. connect(objA, SIGNAL(timeup()), objB1, SLOT(countup()));
    2. connect(objA, SIGNAL(timeup()), objB2, SLOT(countup()));
    To copy to clipboard, switch view to plain text mode 

    But when there gets to be 10 instances of objB, gets to be a little much, especially when that 10 today could be 20 by the end of the project. Was hoping for a way to just say

    Qt Code:
    1. connect(objA, SIGNAL(timeup()), &objB, SLOT(countup()));
    To copy to clipboard, switch view to plain text mode 

    where objB is the defined name of the class before any instances of it are invoked.

    Thanks!

  2. #2
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Connect Signal/Slot to any instance of another class

    Meh, thought maybe I had come up with a solution, but Qt being a real pain, so I can't tell if this will work or not.

    I'm now trying to create the connect statement in the constructor of my objB and passing in a pointer to objA, but the connect statement is throwing errors:

    error: no matching function for call to 'QObject::connect(QWidget*&, const char*, objB* const, const char*)'
    candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
    Both non-matching items have Q_OBJECT instruction and I can't for the life of me figure out why its showing as QWidget*& since I'm passing in a pointer (using "this")

    edit: Thought i should add, objA is derived QGraphicsView, objB is derived QGraphicsItem


    Added after 42 minutes:


    Close, delete or whatever this thread.

    QGraphicsItem does not inheric QObject. Using QGraphicsObject inherits QGraphicsItem and QObject.

    Thus placing the connect signal in the constructor allows any instance of the class to use signal/slot connection
    Last edited by squeegedog; 8th February 2014 at 10:38.

Similar Threads

  1. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 21:42
  2. Replies: 9
    Last Post: 30th June 2010, 20:36
  3. problem connect signal - slot
    By jaca in forum Newbie
    Replies: 13
    Last Post: 9th March 2010, 20:38
  4. Replies: 12
    Last Post: 18th September 2008, 16:04
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 08: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.