Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Connecting custom parent's slot to child's signal in constructor
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Resurrection
    wrote on last edited by
    #1

    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
    {
    Q_OBJECT
    public:
    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

    @
    child(this,this);
    @

    Thanks!

    Secrets are power.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Resurrection
        wrote on last edited by
        #3

        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.

        Secrets are power.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
          wrote on last edited by
          #4

          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.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          https://www.pthinks.com

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved