[英]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. 在Qt中,我有一个MainWindow类和一个
DoSomething()
函数。 Then I created a Job object within MainWindow and tried to call DoSomething within Job's DoItNow()
function. 然后,我在MainWindow中创建了一个Job对象,并尝试在Job的
DoItNow()
函数中调用DoSomething。 But I just don't know how to do it. 但是我只是不知道该怎么做。
MainWindow.h 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 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 Job.h
class Job : public QObject
{
Q_OBJECT
private:
void DoItNow();
public:
explicit CDMcommand(QObject *parent = 0);
}
Job.cpp 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? 如何在* parent中访问非静态公共寄存器?
How to pass *parent to function in job instance? 如何通过* parent在工作实例中起作用?
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
. 您的
Job
是QObject
的子类, MainWindow
也从QObject
间接继承,但MainWindow
和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: 我对Qts信号和插槽机制不太熟悉,这可能是解决问题的方法,但也许我可以为您提供其他解决方案:
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
. 这是行不通的,因为
QObject
没有名为value
成员。 If you can live with Job
s constructor not taking a QObject*
as parameter, then just declare a 如果您可以使用
Job
的构造函数而不使用QObject*
作为参数,则只需声明一个
MainWindow* parentWindow;
as a private member in Job
and change the constructor to 作为
Job
的私有成员并将构造函数更改为
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?
如何从“子”对象方法中调用Qt“父”对象方法?
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. 但是,当然,使两个类彼此之间过于依赖是违反OOP原则的:这两个对象现在紧密相连。 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.
在注释中已经有一个很好的建议:为此使用显式的信号槽机制,或者提供接口以在解耦的对象之间进行交互。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.