Results 1 to 5 of 5

Thread: Create a connection for the parent class

  1. #1
    Join Date
    Apr 2017
    Posts
    30
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Create a connection for the parent class

    Hi all!
    I have a class for the main window, class A and class B, which inherits from class A:

    Qt Code:
    1. MainWindow::MainWindow(){
    2. ...
    3. A a;
    4. connect(&a, SIGNAL(remoteSignal(bool)), this, SLOT(updateStates(bool)));
    5. a.start();
    6. B b;
    7. connect(&b, SIGNAL(remoteSignal(bool)), this, SLOT(updateStates(bool)));
    8. }
    9.  
    10. A::A()
    11. {
    12.  
    13. }
    14. void A::start(){
    15. emit remoteSignal(true)
    16. }
    17.  
    18. B::B()
    19. {
    20. A::start();
    21. }
    To copy to clipboard, switch view to plain text mode 

    If I call the start method from object b for the parent class, there will not be any connections configured. How can I customize them?
    Thanks!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Create a connection for the parent class

    Qt Code:
    1. MainWindow::MainWindow(){
    2. ...
    3. A a;
    4. connect(&a, SIGNAL(remoteSignal(bool)), this, SLOT(updateStates(bool)));
    5. a.start();
    6. B b;
    7. connect(&b, SIGNAL(remoteSignal(bool)), this, SLOT(updateStates(bool)));
    8. }
    To copy to clipboard, switch view to plain text mode 

    This code really does nothing at all. The instances of A and B are created on the stack as temporary variables in the MainWindow constructor and will be destroyed as soon as the destructor exits. When the instances are destroyed, any signal / slot connections involving them will also be destroyed.

    Event though the temporary class A emits a signal through its start() method, this signal will not be acted on until the MainWindow constructor exits and control returns to the event loop. Because A was destroyed at the end of the constructor, the signal will also be removed from the event queue since the object that emitted it no longer exists.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Create a connection for the parent class

    B class emits signal in constructor before any connects. Use QTimer::singleShot with timeout 0 to emit signal.

  4. #4
    Join Date
    Apr 2017
    Posts
    30
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Create a connection for the parent class

    Quote Originally Posted by d_stranz View Post
    [CODE]
    This code really does nothing at all.
    I apologize. The code is very large, so I figuratively wrote it. Of course, pointers to these instances are created in the class declaration, so a and b live as long as the main window class is alive. Connections also work.
    My question is different. Class B is inherited from class A
    Qt Code:
    1. class B: public A
    To copy to clipboard, switch view to plain text mode 
    so if you call a method
    Qt Code:
    1. A::start()
    To copy to clipboard, switch view to plain text mode 
    from an instance of class B, the main window slot will not be called. How can this be done?

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Create a connection for the parent class

    You haven't shown enough code to indicate what might be wrong. I'd suggest that you reduce the problem to these three classes (MainWindow, A, and B) with a bare-bones implementation of the classes, signals, and slots and determine if that works or not. If it doesn't work, post the complete code for the test project and we can look at it. Also be sure you have inserted the Q_OBJECT macro into the definitions of both A and B classes.

    And as Lesiok says, be sure you make all of your signal / slot connections -before- any signals are emitted.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. How to create a SIP connection with Qt for Android?
    By rostamiani in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 11th June 2015, 09:05
  2. Best way for a parent class to know value chosen on child class
    By scarecr0w132 in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2014, 03:40
  3. How to access to methods of parent class
    By tonnot in forum General Programming
    Replies: 3
    Last Post: 29th June 2011, 08:04
  4. Replies: 4
    Last Post: 1st May 2009, 12:00
  5. Queued connection fails on own class
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 2nd December 2008, 19:50

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.