如何在QPalette中使用QPROPERTY?
How to use QPROPERTY in QPalette?
我正在尝试使用样式表中的 Q_PROPERTY 集来更改 QPalette 中的值,这可能吗?例如,如果我在我的 MainWindow 小部件中将 QStyle 设置为 Fusion,是否可以使用此方法更改 Qt::Window 等?
一切都编译正常,但唯一显示的颜色是黑色,所以变量可能填充了垃圾值?据我所知,样式表会覆盖其他所有内容,所以猜测,样式表没有及时加载构造函数?
mainwindow.cpp
#include <QStyleFactory>
#include <QWidget>
#include <QFile>
#include "theme.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
{
QFile File("://stylesheet.qss");
File.open(QFile::ReadOnly);
QString StyleSheet = QLatin1String(File.readAll());
qApp->setStyleSheet(StyleSheet);
Theme *themeInstance = new Theme;
QApplication::setStyle(QStyleFactory::create("Fusion"));
QPalette dp;
dp.setColor(QPalette::Window, QColor(themeInstance->customColor()));
qApp->setPalette(dp);
}
theme.h
#ifndef THEME_H
#define THEME_H
class Theme : public QWidget
{
Q_OBJECT
Q_PROPERTY(QColor customColor READ customColor WRITE setCustomColor DESIGNABLE true)
public:
Theme(QWidget *parent = nullptr);
QColor customColor() const { return m_customColor; }
void setCustomColor(const QColor &c) { m_customColor = c; }
private:
QColor m_customColor;
};
#endif // THEME_H
stylesheet.qss
* { // global only for test purposes
qproperty-customColor: red;
}
QSS 不会自动调用,它们通常在显示小部件时更新,在您的情况下,因为没有显示 themeInstance 不使用样式表。可以使用QStyle
的polish()
方法强制绘画
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr):QMainWindow{parent}{
qApp->setStyleSheet("Theme{qproperty-customColor: red;}");
Theme *themeInstance = new Theme;
qApp->setStyle(QStyleFactory::create("Fusion"));
qApp->style()->polish(themeInstance);
QPalette dp;
dp.setColor(QPalette::Window, QColor(themeInstance->customColor()));
qApp->setPalette(dp);
}
};
我正在尝试使用样式表中的 Q_PROPERTY 集来更改 QPalette 中的值,这可能吗?例如,如果我在我的 MainWindow 小部件中将 QStyle 设置为 Fusion,是否可以使用此方法更改 Qt::Window 等?
一切都编译正常,但唯一显示的颜色是黑色,所以变量可能填充了垃圾值?据我所知,样式表会覆盖其他所有内容,所以猜测,样式表没有及时加载构造函数?
mainwindow.cpp
#include <QStyleFactory>
#include <QWidget>
#include <QFile>
#include "theme.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
{
QFile File("://stylesheet.qss");
File.open(QFile::ReadOnly);
QString StyleSheet = QLatin1String(File.readAll());
qApp->setStyleSheet(StyleSheet);
Theme *themeInstance = new Theme;
QApplication::setStyle(QStyleFactory::create("Fusion"));
QPalette dp;
dp.setColor(QPalette::Window, QColor(themeInstance->customColor()));
qApp->setPalette(dp);
}
theme.h
#ifndef THEME_H
#define THEME_H
class Theme : public QWidget
{
Q_OBJECT
Q_PROPERTY(QColor customColor READ customColor WRITE setCustomColor DESIGNABLE true)
public:
Theme(QWidget *parent = nullptr);
QColor customColor() const { return m_customColor; }
void setCustomColor(const QColor &c) { m_customColor = c; }
private:
QColor m_customColor;
};
#endif // THEME_H
stylesheet.qss
* { // global only for test purposes
qproperty-customColor: red;
}
QSS 不会自动调用,它们通常在显示小部件时更新,在您的情况下,因为没有显示 themeInstance 不使用样式表。可以使用QStyle
polish()
方法强制绘画
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr):QMainWindow{parent}{
qApp->setStyleSheet("Theme{qproperty-customColor: red;}");
Theme *themeInstance = new Theme;
qApp->setStyle(QStyleFactory::create("Fusion"));
qApp->style()->polish(themeInstance);
QPalette dp;
dp.setColor(QPalette::Window, QColor(themeInstance->customColor()));
qApp->setPalette(dp);
}
};