Reputation: 2545
I have a MainWindow class which contains all widgets and I have a MyClass class where I want to access to all widgets placed in the main window (MainWindow class). How can I do this? I try to do it like this (code below) - but it gives me a bunch of errors:
MyClass.h:4: error: 'Ui' has not been declared
MyClass.h:4: error: expected `)' before '*' token
MyClass.h:6: error: 'Ui' has not been declared
MyClass.h:6: error: ISO C++ forbids declaration of 'MainWindow' with no type
MyClass.h:6: error: expected ';' before '*' token
This MainWindow created by QtCreator - it's a standart GUI window created by Qt Creator.
// -=-=-=-=-=-=-=-=-=-=[ MyClass.h ]=-=-=-=-=-=-=-=-=-=-=-=-=-
#include "MainWindow.h"
class MyClass {
public:
MyClass( Ui::MainWindow *ui );
private:
Ui::MainWindow *ui;
};
// -=-=-=-=-=-=-=-=-=-=[ MyClass.cpp ]=-=-=-=-=-=-=-=-=-=-=-=-=-
#include "MyClass.h"
MyClass::MyClass( Ui::MainWindow *ui ){
this->myUI = ui; // myUI is desclared as: Ui::MainWindow *myUI
}
// -=-=-=-=-=-=-=-=-=-=[ MainWindow.h ]=-=-=-=-=-=-=-=-=-=-=-=-=-
....
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
private:
Ui::MainWindow *ui; // this has done by Qt Creator
public:
MyClass *myClass;
}
// -=-=-=-=-=-=-=-=-=-=[ MainWindow.cpp ]=-=-=-=-=-=-=-=-=-=-=-=-=-
#include "MainWindow.h"
MainWindow::MainWindow( QWidget *parent ): QMainWindow( parent ), ui( new Ui::MainWindow ) {
ui->setupUi( this );
myClass = new MyClass( ui ); // my attempt
}
Update: There's useful code in the comment below by beige. It works fine. But not in my case :( When I try to declare MyClass from any class (not from MainWindow.h):
// -=-=-=-=-=-=-=-=-=-=[ MyGLWidget.h ]=-=-=-=-=-=-=-=-=-=-=-=-=-
#include "MyClass.h"
class MyGLWidget : public QGLWidget {
Q_OBJECT
public:
MyClass *myClass;
}
it doesn't work:
myclass.h:9: error: 'Ui' has not been declared
myclass.h:9: error: expected `)' before '*' token
myclass.h:12: error: 'Ui' has not been declared
myclass.h:12: error: ISO C++ forbids declaration of 'MainWindow' with no type
myclass.h:12: error: expected ';' before '*' token
but why? I have declaration of the "ui_mainwindow.h" in my MyClass.h.
Upvotes: 0
Views: 6701
Reputation: 5897
The MyClass header has to include the generated headerfile from the user interface compiler. In your case, it is called ui_mainwindow.h. The constructor of MyClass will take the Ui::MainWindow parameter and save it in a private member variable.
In the constructor of MainWindow you can initialize your MyClass member variable with the MainWindow ui.
// -=-=-=-=-=-=-=-=-=-=[ myclass.h ]=-=-=-=-=-=-=-=-=-=-=-=-=-
#include <ui_mainwindow.h>
class MyClass
{
public:
MyClass(Ui::MainWindow *ui);
private:
Ui::MainWindow *myUi;
};
// -=-=-=-=-=-=-=-=-=-=[ myclass.cpp ]=-=-=-=-=-=-=-=-=-=-=-=-=-
MyClass::MyClass(Ui::MainWindow *ui)
{
// Save the MainWindow-ui as private member of MyClass
this->myUi = ui;
}
// -=-=-=-=-=-=-=-=-=-=[ MainWindow.h ]=-=-=-=-=-=-=-=-=-=-=-=-=-
#include <QMainWindow>
#include "myclass.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
MyClass *myClass; // myClass member dont't has to be public
};
// -=-=-=-=-=-=-=-=-=-=[ MainWindow.cpp ]=-=-=-=-=-=-=-=-=-=-=-=-=-
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Initialize MyClass with the MainWindow-ui
myClass = new MyClass(ui);
}
MainWindow::~MainWindow()
{
delete myClass;
delete ui;
}
Upvotes: 3