Important: Please read the Qt Code of Conduct -

[SOLVED] Connecting custom parent's slot to child's signal in constructor

  • EDIT: Problem was the cyclic include of header. Solved by forward-declaration.

    Here is the problem:

    I have a QObject based custom class which I parse as parent to another QObject based child class. I need to connect the parent's custom slot to child's custom signal in the child's constructor. The reason is that if something goes wrong on initialization of the child class it emits this signal which the parent receives and stops the app.

    I found out I cannot alter the constructor like this:

    class child : public QObject
    child(parentClass *parent = 0);

    as the *parent must be a pointer to QObject part of the parentClass. Is there any easier way I miss here other than parsing the pointer to the parentClass twice, once to its QObject part and once to the full object like this?

    child(parentClass *p, QObject *parent = 0);

    then calling



  • Qt Champions 2017

    I don't see any issue like that. You can alter constructor. You can pass the object twice. You can connect the signal of child to parent slot. It works perfectly. If you want the sample code give me your email id. I will send you the code right away.

  • Thanks, I was looking for more "elegant" solution and I found it actually. My problem was cyclic include of the parent class. The parent class was including the child's header and vice versa so it could not build. So I simply used forward declaration in the header and actual include in the implementation and now I can use the parent class pointer as parent.

  • Qt Champions 2017

    ok. If you had posted this question, this would have been easier. By looking at your first question, it took the ans in different direction. Any happy ending now.

Log in to reply