Qt 样式:如何设置使用 .ui 生成类的小部件的样式?
在我的 .qss 文件中,我想指定小部件的背景颜色,该小部件使用 .ui 文件中生成的类,例如:
#ifndef SPLASH_H
#define SPLASH_H
#include <QWidget>
#include "ui_SplashView.h"
class Splash : public QWidget
{
Q_OBJECT
public:
explicit Splash(QWidget *parent = 0);
signals:
public slots:
private:
Ui::SplashView ui;
};
#endif // SPLASH_H
--
#include "splash.h"
Splash::Splash(QWidget *parent) : QWidget(parent)
{
ui.setupUi(this);
}
ui_SplashView.h 文件本身如下所示:
QT_BEGIN_NAMESPACE
class Ui_SplashView
{
public:
QPushButton *pushButton;
void setupUi(QWidget *SplashView)
{
if (SplashView->objectName().isEmpty())
SplashView->setObjectName(QString::fromUtf8("SplashView"));
SplashView->resize(360, 640);
pushButton = new QPushButton(SplashView);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(70, 30, 75, 23));
retranslateUi(SplashView);
QMetaObject::connectSlotsByName(SplashView);
} // setupUi
void retranslateUi(QWidget *SplashView)
{
SplashView->setWindowTitle(QApplication::translate("SplashView", "Splash", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("SplashView", "PushButton", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class SplashView: public Ui_SplashView {};
} // namespace Ui
在我的 .qss 中,我已经尝试了以下方法,但都不起作用:
*#m_splashView {
背景颜色:蓝色; 这段
*#SplashView {
background:yellow;
background-color:blue;
}
*#Splash {
background:yellow;
background-color:blue;
}
*#splash {
background:yellow;
background-color:blue;
}
*#ui {
background:yellow;
background-color:blue;
}
Ui--SplashView {
background:purple;
background-color:blue;
}
Ui--Splash {
background:purple;
background-color:blue;
}
SplashView {
background:purple;
background-color:blue;
}
Splash {
background:purple;
background-color:blue;
}
代码可以工作,但它太通用了,我想专门针对 Splash 小部件:
QWidget {
background:purple;
}
有什么想法吗?
In my .qss file I'd like to specify the background color of my widget that makes use of the generated class from the .ui file like:
#ifndef SPLASH_H
#define SPLASH_H
#include <QWidget>
#include "ui_SplashView.h"
class Splash : public QWidget
{
Q_OBJECT
public:
explicit Splash(QWidget *parent = 0);
signals:
public slots:
private:
Ui::SplashView ui;
};
#endif // SPLASH_H
--
#include "splash.h"
Splash::Splash(QWidget *parent) : QWidget(parent)
{
ui.setupUi(this);
}
The ui_SplashView.h file itself looks like this:
QT_BEGIN_NAMESPACE
class Ui_SplashView
{
public:
QPushButton *pushButton;
void setupUi(QWidget *SplashView)
{
if (SplashView->objectName().isEmpty())
SplashView->setObjectName(QString::fromUtf8("SplashView"));
SplashView->resize(360, 640);
pushButton = new QPushButton(SplashView);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(70, 30, 75, 23));
retranslateUi(SplashView);
QMetaObject::connectSlotsByName(SplashView);
} // setupUi
void retranslateUi(QWidget *SplashView)
{
SplashView->setWindowTitle(QApplication::translate("SplashView", "Splash", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("SplashView", "PushButton", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class SplashView: public Ui_SplashView {};
} // namespace Ui
In my .qss I've tried the following, but none of them work:
*#m_splashView {
background-color:blue;
}
*#SplashView {
background:yellow;
background-color:blue;
}
*#Splash {
background:yellow;
background-color:blue;
}
*#splash {
background:yellow;
background-color:blue;
}
*#ui {
background:yellow;
background-color:blue;
}
Ui--SplashView {
background:purple;
background-color:blue;
}
Ui--Splash {
background:purple;
background-color:blue;
}
SplashView {
background:purple;
background-color:blue;
}
Splash {
background:purple;
background-color:blue;
}
This code works but it's too generic, I want to specifically target that Splash widget:
QWidget {
background:purple;
}
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了答案。自定义小部件需要实现此功能:
在 .qss 中我输入:
因为这是 .ui 文件中的对象名称。
Found the answer. The custom widget needs to implement this function:
In the .qss I put:
Since that's the object name in the .ui file.