Topo
Topo

Reputation: 5002

Trying to access class in namespace Ui

I'm trying to show a QDialog from a QMainWindow using Qt Creator. The QDialog's name is About. My MainWindow and my QDialog are both in the namespace Ui by default, but I'm getting an error when trying to create a new 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();
}

The error is:

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

This happens in line:

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

What is happening? Is there a struct named About in the namespace Ui?

Upvotes: 1

Views: 2714

Answers (2)

Seb Holzapfel
Seb Holzapfel

Reputation: 3873

You don't really need to use the heap, and you don't use the UI:: declaration of a dialog to instantiate it (That's only a class for the UI of the dialog, not the dialog itself). Use something like this:

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

Assuming you want a modal dialog, usually what an about box is. Otherwise use QDialog::open()

Upvotes: 4

Chris Browet
Chris Browet

Reputation: 4266

namespace Ui {
    class MainWindow; 
    class About;
}

and

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

Upvotes: 2

Related Questions