简体   繁体   中英

How to call to non-static parent function from within child function in Qt/C++?

I have difficulties in understanding how to pass parent object to child. In Qt, I have a MainWindow class and a DoSomething() function. Then I created a Job object within MainWindow and tried to call DoSomething within Job's DoItNow() function. But I just don't know how to do it.

MainWindow.h

class Job;
class MainWindow : public QMainWindow
{
  Q_OBJECT

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

    int value;
    void DoSomething();

  private:
  Job *job;
}

MainWindow.cpp

#include "mainwindow.h"
#include "job.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    job = new Job(this);   // passing this pointer to child
}

void MainWindow::DoSomething() {  // do something }

Job.h

class Job : public QObject
{
  Q_OBJECT

  private:
    void DoItNow();

  public:
    explicit CDMcommand(QObject *parent = 0);
}

Job.cpp

#include "job.h"
#include "mainwindow.h"

Job::Job(QObject *parent) : QObject(parent)
{
  // some setups
  parent->value = 0;    // this is not working
}

void Job::DoItNow()
{
  parent->DoSomething();  // What is the pointer to MainWindow instance?
}
  • How to access non-static public register in *parent?

  • How to pass *parent to function in job instance?

Maybe I missunderstand the question, but I think you are a bit confused about inheritance. Your Job is a child class of QObject and MainWindow indirectly inherits also form QObject , but there is no direct relation between MainWindow and Job . I am not too familiar with Qts signal and slot mechanism, which is probably the way to go here, but maybe I can offer you a different solution:

Job::Job(QObject *parent) : QObject(parent)
{
  // some setups
  parent->value = 0;    // this is not working
}

This is not working, because QObject has no member called value . If you can live with Job s constructor not taking a QObject* as parameter, then just declare a

MainWindow* parentWindow;

as a private member in Job and change the constructor to

Job::Job(MainWindow *parentWindow) : QObject(parentWindow)
{
  // some setups
  parentWindow->value = 0;    // this will work now
}

then also

void Job::DoItNow()
{
  parentWindow->DoSomething();  
}

will work without problems.

How can I call the Qt 'parent' object method from the 'child' object method?

The safe and simple way to do it:

void Job::DoItNow()
{
   // first evaluate the pointer: is that of type we expect?
   MainWindow* pMainWindow = qobject_cast<MainWindow*>(parent());
   if (pMainWindow)
       pMainWindow->DoSomething();  // MainWindow::DoSomething must be exposed to class Job
}

But of course making two classes dependent on each other too much is a violation of OOP principles: these two objects become tightly coupled now. And there is already a good suggestion in comments: use an explicit signal-slot mechanism for that or providing the interface to interact between decoupled objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM