2011-01-26 176 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的代码。