2012-10-09 38 views
0

我有一個包含所有小部件的MainWindow類,我有一個MyClass類,我想要訪問放置在主窗口(MainWindow類)中的所有小部件。我怎樣才能做到這一點?我嘗試做這樣的(下面的代碼) - 但它給了我一堆錯誤:無法訪問我的UI的小部件

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創建這個主窗口 - 它是由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」

+2

你會得到什麼錯誤? – leemes

+0

這是你的確切代碼嗎?你會得到運行時錯誤或編譯器錯誤? – Bart

+0

就行了:「public:MyClass(Ui :: MainWindow * ui);」的MyClass.h,我在'*'標記之前給出了一個錯誤:「error:expected')'。此外,編譯器聲明在這一行上:「private:Ui :: MainWindow * ui;」:「錯誤:ISO C++禁止聲明'MainWindow'沒有類型」。 – JavaRunner

回答

3

MyClass頭文件必須包含從用戶界面compi生成的頭文件LER。在你的情況下,它被稱爲ui_mainwindow.h。 MyClass的構造函數將採用Ui :: MainWindow參數並將其保存在私有成員變量中。

在MainWindow的構造函數中,您可以使用MainWindow ui初始化您的MyClass成員變量。

// = = - = - = - = - = - = - = - = - = - = = = = = - = = - = - = - = -

#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; 
} 
+0

這是很棒的代碼......你可能也想添加一個解釋。;) – Bart

+0

是的,我想要。我已經添加了一些評論。還使MainWindow的myClass成員變爲private,並在析構函數中將其刪除。我想,那會做:)。 – tomvodi

+0

更好,但也許在代碼實際演示的解決方案的頂部添加一兩句話。這也可能會阻止它在我們的審查工具中顯示爲僅用於代碼。 – Bart