Qt:如何将*.ui文件和*.qss文件与MainWindow类实例关联?

Qt:如何将*.ui文件和*.qss文件与MainWindow类实例关联?,qt,Qt,这仍然有点神秘。我使用了样式表Qt示例应用程序,其中演示了*.ui和*.qss文件的用法 它们有一个在*.ui中设计的主窗口类。但是,该代码根本不包含对任何*.ui或*.qs的引用,而且它们在运行时是关联的。我不明白怎么做 这是初始化主窗口的代码 int main(int argc, char *argv[]) { Q_INIT_RESOURCE(stylesheet); QApplication app(argc, argv); MainWindow window;

这仍然有点神秘。我使用了样式表Qt示例应用程序,其中演示了*.ui和*.qss文件的用法

它们有一个在*.ui中设计的主窗口类。但是,该代码根本不包含对任何*.ui或*.qs的引用,而且它们在运行时是关联的。我不明白怎么做

这是初始化主窗口的代码

int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(stylesheet);
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
}
这是主窗口的代码

*.h:

\ifndef主窗口
#定义主窗口
#包括
#包括“ui_main window.h”
类样式表编辑器;
类主窗口:公共QMainWindow
{
Q_对象
公众:
主窗口();
专用插槽:
在_editStyleAction_triggered()上无效;
关于触发的按钮()无效;
私人:
样式表编辑器*样式表编辑器;
Ui::主窗口Ui;
};
#恩迪夫
*.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."));
}
包括
#包括“mainwindow.h”
#包括“stylesheeteditor.h”
MainWindow::MainWindow()
{
ui.setupUi(这个);
ui.namelab->setProperty(“类”,“强制QLabel”);
styleSheetEditor=新的styleSheetEditor(此);
statusBar()->addWidget(新的QLabel(tr(“就绪”));
连接(ui.exitAction、信号(triggered())、qApp、插槽(quit());
连接(ui.aboutqtraction、信号(triggered())、qApp、插槽(aboutQt());
}
void main window::on_editStyleAction_triggered()
{
样式表编辑器->显示();
样式表编辑器->激活窗口();
}
void主窗口::on_aboutAction_已触发()
{
QMessageBox::about(this,tr(“about样式表”),
tr(“样式表示例显示如何设置小部件的样式”
“使用。单击文件|编辑样式表以弹出”
样式编辑器,并选择现有样式表或设计
“你自己的。”);
}

有人能解释一下为什么它会在我的参考资料中被一个*.ui文件的内容唤醒吗?

ui文件由
uic
处理,以生成
ui\u mainwindow.h
文件。查看此文件,您将看到用于构建QMainWindow的代码。

UI文件没有直接关联。Qt构建过程(通常由QGuy完成)包括使用Qt中包含的UIC工具从*.UI文件生成C++代码。它会生成包含的“ui_mainwindow.h”。它包含您显式使用的Ui::MainWindow类,因此它并不神秘。因此,您的代码不直接使用*.ui文件,而是使用从它们生成的内容


但是,我不确定*.qs,因为我没有使用它们。但是您需要调用Q_INIT_RESOURCE()宏,资源文件可能包含对*.qss文件的引用。如果是,则表示该文件包含在Qt资源系统中,这是应用程序本地的一种虚拟文件系统。

wow。谢谢对于来自“普通”C++/GDI/Win32或C#/.NET的人来说,这是一种新的思维方式,但我会掌握其中的诀窍。
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."));
}