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 Object from child
Forum Updated to NodeBB v4.3 + New Features

[Solved] Accessing Object from child

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 2.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.
  • B Offline
    B Offline
    bareil76
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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...

      --- 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
      0
      • B Offline
        B Offline
        bareil76
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          NicuPopescu
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • N Offline
            N Offline
            NicuPopescu
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bareil76
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                [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);
                }
                

                @

                --- 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
                0
                • N Offline
                  N Offline
                  NicuPopescu
                  wrote on last edited by
                  #8

                  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();
                  }@

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    bareil76
                    wrote on last edited by
                    #9

                    Thanks a lot! Both of your method work great.

                    However, what is the most correct approach?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        NicuPopescu
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          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.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          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