c++从函数中访问父窗口小部件

时间:2021-12-28 15:07:26

I am new to C++ and Qt and I am trying to access a widget from a parent class.

我是c++和Qt的新手,我正在尝试从父类中访问一个小部件。

Here is my mainwindow.cpp

这是我mainwindow.cpp

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

I have another class, and I am trying to access a widget from "ui" in that class. Like so:

我有另一个类,我试图从这个类中的“ui”中访问一个小部件。像这样:

DashBoard::DashBoard( MainWindow *parent ) : QObject( parent ) 
{
}

void DashBoard::select(  ) 
{
    parent->ui->menuSL->setCurrentIndex(0);
}

This gives me an error saying that the methods and fields could not be resolved. But when I put the line parent->ui->menuSL->setCurrentIndex(0); in the constructor, there is no problem.

这给了我一个错误,说方法和字段不能被解析。但是当我代入line parent->ui->menuSL->setCurrentIndex(0)时;在构造函数中,没有问题。

Would someone please point out my mistake?

谁能指出我的错误吗?

2 个解决方案

#1


3  

From the code one can infer that the class DashBoard inherits QObject. The parent field of a QObject is defined as a pointer to a QObject so when you call parent->ui->menuSL->setCurrentIndex(0); inside a method of the class DashBoard you're assuming that QObject defines a member called ui which is not true.

从代码中可以推断类仪表板继承了QObject。QObject的父字段定义为指向QObject的指针,所以当您调用父->ui->menuSL-> currentindex(0)时;在类仪表板的方法中,您假定QObject定义了一个名为ui的成员,这不是真的。

Just cast parent this way:

就这样对父母说:

((MainWindow*)(parent()))->ui->menuSL->setCurrentIndex(0);

or this one:

或者这一个:

MainWindow* parent = qobject_cast<MainWindow*>(this->parent());
// check parent is not null
parent->ui->menuSL->setCurrentIndex(0);

In the constructor you don't see the error because parent is defined as a pointer to an object of the class MainWindow and then passed to the QObject's constructor.

在构造函数中,您看不到错误,因为父类被定义为指向类MainWindow的对象的指针,然后传递给QObject的构造函数。

Don't forget to make ui public and to include the auto generated UI header if using Qt Designer (in your case probably "ui_mainwindow.h") in the DashBoard cpp file.

如果在仪表板cpp文件中使用Qt设计器(在您的例子中可能是“ui_mainwindow.h”),请不要忘记将ui公开,并包含自动生成的ui头。

NOTE: I'm just trying to answer your question but I encourage you to review the way you're doing this. There are several ways of achieving the same with a more consistent OO design.

注意:我只是想回答你的问题,但我鼓励你回顾一下你的做法。有几种方法可以通过更加一致的OO设计实现相同的目标。

#2


2  

Within your select method, you try to use a variable named parent. But you need the QObject::parent() method.

在select方法中,尝试使用名为parent的变量。但是您需要QObject::parent()方法。

Furthermore, you need to cast the parent to QMainWindow.

此外,您需要将父进程强制转换为QMainWindow。

void DashBoard::select(  ) {
  QMainWindow* parent = qobject_cast<QMainWindow>(this->parent());
  if (parent == 0) { return; } // or some other error handling
  parent->ui->menuSL->setCurrentIndex(0);
}

You can only access ui if it is public.

您只能在ui是公共的时候访问它。

At all, I think you should provide a method within you MainWindow class which does the operation you want.

总之,我认为您应该在主窗口类中提供您想要的操作的方法。

#1


3  

From the code one can infer that the class DashBoard inherits QObject. The parent field of a QObject is defined as a pointer to a QObject so when you call parent->ui->menuSL->setCurrentIndex(0); inside a method of the class DashBoard you're assuming that QObject defines a member called ui which is not true.

从代码中可以推断类仪表板继承了QObject。QObject的父字段定义为指向QObject的指针,所以当您调用父->ui->menuSL-> currentindex(0)时;在类仪表板的方法中,您假定QObject定义了一个名为ui的成员,这不是真的。

Just cast parent this way:

就这样对父母说:

((MainWindow*)(parent()))->ui->menuSL->setCurrentIndex(0);

or this one:

或者这一个:

MainWindow* parent = qobject_cast<MainWindow*>(this->parent());
// check parent is not null
parent->ui->menuSL->setCurrentIndex(0);

In the constructor you don't see the error because parent is defined as a pointer to an object of the class MainWindow and then passed to the QObject's constructor.

在构造函数中,您看不到错误,因为父类被定义为指向类MainWindow的对象的指针,然后传递给QObject的构造函数。

Don't forget to make ui public and to include the auto generated UI header if using Qt Designer (in your case probably "ui_mainwindow.h") in the DashBoard cpp file.

如果在仪表板cpp文件中使用Qt设计器(在您的例子中可能是“ui_mainwindow.h”),请不要忘记将ui公开,并包含自动生成的ui头。

NOTE: I'm just trying to answer your question but I encourage you to review the way you're doing this. There are several ways of achieving the same with a more consistent OO design.

注意:我只是想回答你的问题,但我鼓励你回顾一下你的做法。有几种方法可以通过更加一致的OO设计实现相同的目标。

#2


2  

Within your select method, you try to use a variable named parent. But you need the QObject::parent() method.

在select方法中,尝试使用名为parent的变量。但是您需要QObject::parent()方法。

Furthermore, you need to cast the parent to QMainWindow.

此外,您需要将父进程强制转换为QMainWindow。

void DashBoard::select(  ) {
  QMainWindow* parent = qobject_cast<QMainWindow>(this->parent());
  if (parent == 0) { return; } // or some other error handling
  parent->ui->menuSL->setCurrentIndex(0);
}

You can only access ui if it is public.

您只能在ui是公共的时候访问它。

At all, I think you should provide a method within you MainWindow class which does the operation you want.

总之,我认为您应该在主窗口类中提供您想要的操作的方法。