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] Accessing a parent widget's variables
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Accessing a parent widget's variables

Scheduled Pinned Locked Moved General and Desktop
22 Posts 7 Posters 46.4k 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.
  • ? Guest
    28 Mar 2012, 21:09

    [quote author="planarian" date="1332968041"]
    That's the source of my confusion: I simply didn't realize that QObject can never take more than one pointer.

    As it currently stands, parent.h includes child.h, parent.cpp includes parent.h, child.h has a "class parent" forward declaration, and child.cpp includes child.h. So I don't believe your diagnosis is correct. [/quote]

    It is YOU (or eventually Creator) that constructs a QObject inside the QObject derived class, if inheritance is indirect the previous base class is responsble for it, and the parent pointer is passed down the hierarchy until it reaches the bottom base QObject class. Unlike with signals and slots, there is no magic here, no code is generated for you behind the scenes. QObject itself has only 3 constructors, and neither of them can take more than one parent parameter, and the parent parameter is not automatically passed into QObject, it is done in the constructor of the first class that inherits QObject by code, no matter how deep it is before your own class.

    Your forward declaration should work the way you have described your situation. Either that is not the case, or there is some other issue, you are best posting your code and the exact error message that goes with it, so we can see what you are trying to do and if necessary test it ourselves. For me accessing parent members from a child totally works.

    @// parent header
    #ifndef MYPARENT_H
    #define MYPARENT_H

    #include <QObject>

    class MyParent : public QObject
    {
    Q_OBJECT
    public:
    explicit MyParent(QObject *parent = 0);
    int someVariable;
    };
    #endif // MYPARENT_H

    // parent cpp
    #include "myclass.h"

    MyParent::MyParent(QObject *parent) :
    QObject(parent), someVariable(7) {} // some variable initialized to 7

    // child header
    #ifndef MYCHILD_H
    #define MYCHILD_H

    #include <QObject>
    class MyParent; //forward declaration

    class MyChild : public QObject
    {
    Q_OBJECT
    public:
    explicit MyChild(MyParent *parent = 0);
    };
    #endif // MYCHILD_H

    // child cpp
    // parent header included here in the child cpp
    #include "mychild.h"
    #include "myparent.h"

    MyChild::MyChild(MyParent *parent) :
    QObject(parent)
    {
    parent->someVariable = 666; // access parent member through pointer and change from 7 to 666
    }

    //main
    #include <QtCore/QCoreApplication>
    #include <QDebug>
    #include "myparent.h"
    #include "mychild.h"

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

     MyParent parent;
     qDebug() << parent.someVariable; // prints out 7
    
     MyChild child(&parent);
    
     qDebug() << parent.someVariable; // prints out 666
     qDebug() << dynamic_cast <MyParent *>(child.parent())->someVariable; // casting the QObject * to MyParent * using C++ dynamic cast, no checking, will crash if cast fails, prints out 666
     
     MyParent * temp = qobject_cast <MyParent *>(child.parent()); //create a local pointer to parent from casting child parent() method with Qt qobject cast
     if (temp) { //check if cast succeeded 
            temp->someVariable = 333;
            qDebug() << parent.someVariable; // prints out 333
     }
    
     return a.exec(&#41;;
    

    }@

    M Offline
    M Offline
    Mischa schirmer
    wrote on 4 Oct 2018, 14:11 last edited by
    #21

    Very nice! This example works like a charm and has saved me a lot of head aches. Thank you!

    1 Reply Last reply
    0
    • ? Guest
      27 Mar 2012, 19:26

      Well, obviously you need a pointer to your parent object, not to QObject which is passed in the constructor or returned by parent(). It is your class that inherits QObject that has the variable, what you need is to cast that QObject pointer to your custom class type. Without this procedure, you are limited to the facilities of QObject. You can use either qobject_cast or dynamic_cast just to introduce some runtime type safety, if the cast fails it will return zero, whereas a plain cast will work in the blind and crash you app if something goes wrong.

      @YourParentClass *myParent= qobject_cast<YourParentClass *>(parent()); // or without () if you are in the constructor
      if (myParent)
      myParent->someVariable ...@

      ... or alternatively, if you don't need the pointer object you can simply do:

      @qobject_cast<YourParentClass *>(parent())->someVariable ... @

      Note with the second approach you will neither get autocomplete in the IDE, nor is there a way to check if the cast failed for some reason. Since the outcome of the cast is not known until runtime, if you call someVariable to a NULL pointer, you are likely to crash.

      M Offline
      M Offline
      ma_veasna
      wrote on 9 Mar 2020, 07:57 last edited by
      #22

      Thank you so much . Your answer helped me fix my problem which I was stuck for almost a week. big thank again.

      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