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. parent() not returning the "parent" QWidget given to constructor, why?
Qt 6.11 is out! See what's new in the release blog

parent() not returning the "parent" QWidget given to constructor, why?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 1.1k Views 2 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.
  • D Offline
    D Offline
    DasCapschen
    wrote on last edited by
    #1

    I have a problem, the parent() function does not return the same object that I passed into the constructor. Thus, when I ((MainWindow*)parent())->function(); I get a segfault... not nice...

    so I checked why this happens with a bunch of qDebug()s... this is what I got...

    MainWindow Contructor, this                      == 0x7ffc53ce97f0
    FileList Constructor, parent()                   == 0x7ffc53ce97f0
    FileList Function, parent()                      == 0x557b8c8c9170
    parent()->parent()->parent()->parent()->parent() == 0x7ffc53ce97f0
    

    so... where do 4 parent objects come from, that I never created? And it seems they only spawn into existance AFTER my constructor ran... so it's not like it's the superclass constructor that causes this...

    I mean I guess I could just save the parent I give to the constructor as a member variable (or just call parent() five times...), but doesn't that kind of defeat the purpose of parent()?

    I'd like to know, WHY does this happen? And what can I do about it?

    raven-worxR VRoninV 2 Replies Last reply
    0
    • D DasCapschen

      I have a problem, the parent() function does not return the same object that I passed into the constructor. Thus, when I ((MainWindow*)parent())->function(); I get a segfault... not nice...

      so I checked why this happens with a bunch of qDebug()s... this is what I got...

      MainWindow Contructor, this                      == 0x7ffc53ce97f0
      FileList Constructor, parent()                   == 0x7ffc53ce97f0
      FileList Function, parent()                      == 0x557b8c8c9170
      parent()->parent()->parent()->parent()->parent() == 0x7ffc53ce97f0
      

      so... where do 4 parent objects come from, that I never created? And it seems they only spawn into existance AFTER my constructor ran... so it's not like it's the superclass constructor that causes this...

      I mean I guess I could just save the parent I give to the constructor as a member variable (or just call parent() five times...), but doesn't that kind of defeat the purpose of parent()?

      I'd like to know, WHY does this happen? And what can I do about it?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @DasCapschen
      this depends on your code.
      If you call QMainWindow::setCentralWidget() for example your widget is reparented. No matter what parent you passed at construction time

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      8
      • D DasCapschen

        I have a problem, the parent() function does not return the same object that I passed into the constructor. Thus, when I ((MainWindow*)parent())->function(); I get a segfault... not nice...

        so I checked why this happens with a bunch of qDebug()s... this is what I got...

        MainWindow Contructor, this                      == 0x7ffc53ce97f0
        FileList Constructor, parent()                   == 0x7ffc53ce97f0
        FileList Function, parent()                      == 0x557b8c8c9170
        parent()->parent()->parent()->parent()->parent() == 0x7ffc53ce97f0
        

        so... where do 4 parent objects come from, that I never created? And it seems they only spawn into existance AFTER my constructor ran... so it's not like it's the superclass constructor that causes this...

        I mean I guess I could just save the parent I give to the constructor as a member variable (or just call parent() five times...), but doesn't that kind of defeat the purpose of parent()?

        I'd like to know, WHY does this happen? And what can I do about it?

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @DasCapschen said in parent() not returning the "parent" QWidget given to constructor, why?:

        WHY does this happen?

        Layouts and container widgets re-parent anything that is passed to them.

        And what can I do about it?

        Good OOP design works as a 1 way tree, the parent knows about the child but the child assumes and knows nothing about the parent so ((MainWindow*)parent())->function(); is very bad design. the child should emit a signal and the parent should connect to that signal to run the function() slot

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        5
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          As the others says, this should be done via signal and slots for a much better design.

          However, the cast is also suboptimal and dangerous. ( as your segfault tells you :)
          This will not crash if parent is not what you think.

          MainWindow* mywin = qobject_cast<MainWindow* > ( parent() ) ;
          if (mywin) mywin->function();
          
          1 Reply Last reply
          4

          • Login

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