c++ - 尝试访问命名空间 Ui 中的类

标签 c++ qt namespaces qt4

我正在尝试使用 Qt Creator 从 QMainWindow 显示 QDialog。 QDialog 的名称是关于。默认情况下,我的 MainWindow 和 QDialog 都在命名空间 Ui 中,但在尝试创建新的 About 时出现错误。

主窗口.h

#include <QMainWindow>
#include "about.h"

namespace Ui {
    class MainWindow; 
}

class MainWindow : public QMainWindow {
    Q_OBJECT

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

protected:
    void changeEvent(QEvent *e);

private slots:
    void on_actionAbout_activated();

private:
    Ui::MainWindow *ui;
    Ui::About *about; 
};

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

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

void MainWindow::on_actionAbout_activated()
{
    this->about = new Ui::About(this);
    this->about->show();
}

错误是:

invalid use of incomplete type ‘struct Ui::About’

这发生在一行中:

this->about = new Ui::About(this);

发生了什么事?命名空间 Ui 中是否有名为 About 的结构体?

最佳答案

您实际上不需要使用堆,也不需要使用对话框的 UI:: 声明来实例化它(那只是 UI 对话框,而不是对话框本身)。使用这样的东西:

About dlg(this);
dlg.exec();

假设您想要一个模态对话框,通常是关于框。否则使用 QDialog::open()

关于c++ - 尝试访问命名空间 Ui 中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9651558/

相关文章:

c++ - SFINAE 的问题

qt - 退出 Qt 程序的正确方法?

c# - MVC 5 - Controller 看不到我需要的命名空间

c++ - 弱资源与工厂设计模式

c++ - 如何在 C++ 构建中嵌入 JSON 文件

c++ - 启动、停止和发送参数以将 Python 脚本与 C++ 应用程序分开的最佳方法?

objective-c - Objective-C : having name collision while adding a framework in my app via cocoapod

具有动态命名空间的 XSLT 转换

C++:为什么这个 sync() 在这个组合模式中不起作用?

qt - 使用 QNetworkAccessManager 支持 https。运行时遇到 SslErrors