c++ - Qt 无法从继承类访问 *ui 指针

标签 c++ qt user-interface inheritance objective-c++

我正在尝试编写一个应用程序,其中我将有一个通用对话窗口和特定对话窗口,这些对话窗口将从通用对话窗口继承一些基本功能。我不确定这是最好的方法,但我就是这样做的(CGenericProject 类是从 Qt Creator 中的 Dialog 模板创建的):

CGenericProject.h:

#include <QDialog>

namespace Ui {
class CGenericProject;
}

class CGenericProject : public QDialog
{
    Q_OBJECT

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

protected:
    Ui::CGenericProject *ui;
};

CGenericProject.cpp:

#include "cgenericproject.h"
#include "ui_cgenericproject.h"

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

CGenericProject::~CGenericProject()
{
    delete ui;
}

CEisProject.h:

#include "cgenericproject.h"

class CEisProject : public CGenericProject
{
public:
    CEisProject();
    ~CEisProject();

};

CEisProject.cpp:

#include "ceisproject.h"

CEisProject::CEisProject()
{
    ui-> NO ACCESS
}

CEisProject::~CEisProject()
{

}

如您在 CEisProject.cpp 文件中所见,我无权访问从 CGenericProject 继承的 ui 字段,即使它是 protected 。我的意思是,我看到了 ui 本身,但是我没有看到它的方法和成员。我将在那里定义的任何其他变量都可以访问。怎么了?我将不胜感激以这种方式提供的所有帮助。

最佳答案

你必须添加行

#include "ui_cgenericproject.h"

CEisProject.cpp 文件。

CGenericProject.h 文件包含在CEisProject.h 中,但是CEisProject.h 没有访问CGenericProject 的权限。 cpp。在您的基类的 header 中,您只有 Ui::CGenericProject 的前向声明,并且您将其文件包含在 .cpp 中。所以CGenericProject.cpp知道这个类的实现。

但是 CEisProject.cpp 无法访问它,因此您必须在此处再次包含该文件。

注意 您的前向声明令人困惑,您应该正确缩进。此外,在您的代码中添加一些注释以明确谁在阅读它,您正在使用两个具有相同名称的不同类。

关于c++ - Qt 无法从继承类访问 *ui 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30953379/

相关文章:

c++ - Cmake 找不到 QGLWidget

php - 将 jQuery UI 对话框模态表单数据插入 mySQL

c++ - 使用 Eclipse CDT 进行复杂配置

c++ - 如何将 recursive_directory_iterator 与算法库一起使用,尤其是 std::remove_if?

c++ - 无法在Qt中编译,表示错误1和错误2

asp.net - 我的密码文本框显示字符

c# - 使用 Selenium C# 的 Internet Explorer 自动化测试

c++ - 从 std::shared_ptr<void> 初始化 std::shared_ptr<T>

c++ - 将 Makefile 翻译成 CMakeLists;无法加载共享库

qt - 是否可以在 Qt Creator 中将 Qt 样式表与提升的小部件一起使用?