开发者

Qt: How does *.ui files and *.qss files get associated with MainWindow class instance?

It's still somewhat of a mystery. I used the stylesheet Qt example application, where it demonstrates usage of *.ui and *.qss files.

They have a main window class which is designed in a *.ui. The code however contains no references at all to any *.ui or *.qss and yet they are associated at runtime. I can't understand how.

this is the code to initialize the main window;

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

This is the code for mainwindow...

*.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."));
}

Can anyone explain why it awakens with the contents of a *.ui file in my resources?


The UI files aren't associated directly. The Qt build process (usually done by qmake) includes generating C++ code from *.ui files using the UIC tool included with Qt. It generates that "ui_mainwindow.h" which you include. It contains the Ui::MainWindow class which you explicitly use, so it's no mystery. So your code doesn't use *.ui files directly, but it does use something that is generated from them.

I'm not sure about *.qss, though, as I haven't used them. But you have a call to Q_INIT_RESOURCE() macro, perhaps the resources file contains a reference to the *.qss file(s). If it does, then it means that that file is included in the Qt resource system which is a kind of virtual file system local to the application.


The ui file is processed by uic in order to generate the ui_mainwindow.h file. Look at this file, and you will see the code used to build your QMainWindow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜