无法访问我的UI小部件

时间:2012-10-09 14:03:54

标签: c++ qt

我有一个包含所有小部件的MainWindow类,我有一个MyClass类,我想访问放置在主窗口(MainWindow类)中的所有小部件。我怎样才能做到这一点?我尝试这样做(下面的代码) - 但它给了我一堆错误:

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

这个由QtCreator创建的MainWindow - 它是由Qt Creator创建的标准GUI窗口。

// -=-=-=-=-=-=-=-=-=-=[ 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
}

更新 米色下面的评论中有用的代码。它工作正常。但不是在我的情况下:(当我尝试从任何类(而不是从MainWindow.h)声明MyClass时:

// - = - = - = - = - = - = - = - = - = - = [MyGLWidget.h] = - = - = - = - = - = - = - = - = - = - = - = - = -

#include "MyClass.h"
class MyGLWidget : public QGLWidget {
   Q_OBJECT
   public:
     MyClass *myClass;
}

它不起作用:

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

但为什么呢?我在MyClass.h中声明了“ui_mainwindow.h”。

1 个答案:

答案 0 :(得分:3)

MyClass标头必须包含用户界面编译器生成的头文件。在您的情况下,它被称为ui_mainwindow.h。 MyClass的构造函数将获取Ui :: MainWindow参数并将其保存在私有成员变量中。

在MainWindow的构造函数中,您可以使用MainWindow ui初始化MyClass成员变量。

// - = - = - = - = - = - = - = - = - = - = [[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;
}