【问题标题】:Trying to access class in namespace Ui尝试访问命名空间 Ui 中的类
【发布时间】:2012-03-11 01:04:01
【问题描述】:

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

MainWindow.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; 
};

MainWindow.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 的结构体?

【问题讨论】:

    标签: c++ qt namespaces qt4


    【解决方案1】:

    你真的不需要使用堆,也不需要使用对话框的UI::声明来实例化它(那只是对话框的UI的一个类,不是对话框本身)。使用这样的东西:

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

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

    【讨论】:

      【解决方案2】:
      namespace Ui {
          class MainWindow; 
          class About;
      }
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-29
        • 2021-01-02
        • 1970-01-01
        相关资源
        最近更新 更多