2011-01-26 23 views
0

這仍然有點神祕。我使用了樣式表Qt示例應用程序,它演示了* .ui和* .qss文件的用法。Qt:* .ui文件和* .qss文件如何與MainWindow類實例關聯?

他們有一個主窗口類,它是在* .ui中設計的。然而,代碼根本不包含任何* .ui或* .qss的引用,但它們在運行時關聯。我無法理解如何。

這是初始化主窗口的代碼;

int main(int argc, char *argv[]) 
{ 
    Q_INIT_RESOURCE(stylesheet); 
    QApplication app(argc, argv); 
    MainWindow window; 
    window.show(); 
    return app.exec(); 
} 

這是主窗口的代碼...

的* .h:

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QtGui> 

#include "ui_mainwindow.h" 

class StyleSheetEditor; 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    MainWindow(); 

private slots: 
    void on_editStyleAction_triggered(); 
    void on_aboutAction_triggered(); 

private: 
    StyleSheetEditor *styleSheetEditor; 
    Ui::MainWindow ui; 
}; 

#endif 

*的.cpp:

include <QtGui> 

#include "mainwindow.h" 
#include "stylesheeteditor.h" 

MainWindow::MainWindow() 
{ 
    ui.setupUi(this); 

    ui.nameLabel->setProperty("class", "mandatory QLabel"); 

    styleSheetEditor = new StyleSheetEditor(this); 

    statusBar()->addWidget(new QLabel(tr("Ready"))); 

    connect(ui.exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); 
    connect(ui.aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt())); 
} 

void MainWindow::on_editStyleAction_triggered() 
{ 
    styleSheetEditor->show(); 
    styleSheetEditor->activateWindow(); 
} 

void MainWindow::on_aboutAction_triggered() 
{ 
    QMessageBox::about(this, tr("About Style sheet"), 
     tr("The <b>Style Sheet</b> example shows how widgets can be styled " 
      "using <a href=\"http://qt.nokia.com/doc/4.5/stylesheet.html\">Qt " 
      "Style Sheets</a>. Click <b>File|Edit Style Sheet</b> to pop up the " 
      "style editor, and either choose an existing style sheet or design " 
      "your own.")); 
} 

任何人都可以解釋爲什麼它與喚醒我的資源中* .ui文件的內容?

回答

1

UI文件不直接關聯。 Qt構建過程(通常由qmake完成)包括使用Qt附帶的UIC工具從* .ui文件生成C++代碼。它生成你所包含的「ui_mainwindow.h」。它包含你明確使用的Ui :: MainWindow類,所以它不是神祕的。所以你的代碼不直接使用* .ui文件,但它確實使用了從它們生成的東西。

雖然我不確定* .qss,因爲我沒有使用它們。但是,您調用了Q_INIT_RESOURCE()宏,可能資源文件包含對* .qss文件的引用。如果是這樣,那麼這意味着該文件包含在Qt資源系統中,該系統是應用程序本地的一種虛擬文件系統。

+0

哇。謝謝。對於來自「普通」C++/GDI/Win32或C#/ .NET的用戶來說,這是一種全新的思維方式,但我會掌握它的。 – JasonGenX 2011-01-26 20:31:39

0

該UI文件由uic處理,以便生成ui_mainwindow.h文件。看看這個文件,你會看到用於構建QMainWindow的代碼。