[Solved] Accessing Object from child



  • Hi all,

    I have a basic problem which should be easy to solve, but It won't work.

    I have a mainwindow class with two other classes. "first" and "second"

    @namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    first *fristClass;
    second *secondClass;

    public slots:

    signals:

    private slots:

    protected:

    protected slots:

    };@

    In my mainwindow class, I have the following constructor

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    setupActions();

    firstClass= new first();
    secondClass=new second();--
    

    }@

    In fact, secondClass NEEDS first class to function... so I would need to include the firstClass pointer to its constructor, but it won't work!!!!

    How do I do that in Qt?


  • Moderators

    can you please be more specific.
    whats your exact problem?

    when you create a instance of "first" before "second" you can easily pass it by the constructor...



  • Yes... I know.. but can you explain HOW to pass the pointer in the constructor.

    I tried adding parent &p in the argument and it did not worked.



  • second's constructor's parameters list is empty, so how would you think to pass the first's pointer into constructor ?



  • bq. I tried adding parent &p in the argument and it did not worked

    as first and seconds are encapsulated in the same class their type object's can be directly passed from one to another ... so in arg list must be (first &f) ... it would work through parent's type, as (MainWindow &p) but you need a public geter for first ... this is awkward anyway



  • what do you mean it is awkward? How would you proceed?


  • Moderators

    [quote author="bareil76" date="1381322725"]Yes... I know.. but can you explain HOW to pass the pointer in the constructor.

    I tried adding parent &p in the argument and it did not worked.[/quote]

    depends on your class definition of "second":

    @
    #include "first.h"

    class Second
    {
    public:
    Second(First* first) : m_First(first)
    {
    }

    void doSomethingWithFirst()
    {
         if( m_First )
             m_First->callMethod();
    }
    

    protected:
    First* m_First;
    }
    @

    Then you can do this in your MainWindow constructor:
    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    setupActions();

        firstClass= new first();
        secondClass=new second(firstClass);
    }
    

    @



  • bq. what do you mean it is awkward? How would you proceed?

    in .h

    @namespace Ui {
    class MainWindow;
    }

    class MainWindow; //<-- forward declaration

    class first
    {

    };

    class second
    {
    first *_f;

    public:
    second(MainWindow *p);
    };

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    first *getFirst(){ return f;}

    private:
    Ui::MainWindow *ui;
    first *f;
    second *s;

    protected:
    bool eventFilter(QObject *obj, QEvent *event);
    };@

    in .cpp you must define the second's constructor:

    @...
    second::second(MainWindow *p)
    {
    _f = p->getFirst();
    }@



  • Thanks a lot! Both of your method work great.

    However, what is the most correct approach?


  • Lifetime Qt Champion

    Hi,

    raven-worx method is cleaner, your second class doesn't need to know two different classes, especially since the only purpose of MainWindow would be to provide the pointer to the "first" object.



  • no doubt! ... my point was to unchain a little bit the imagination and in programming you need this a lot :)


  • Lifetime Qt Champion

    Sure you need imagination, but clean code should be a top priority, without that you get quickly a lot of useless tight couplings and messy code that become nightmares to maintain.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.