【问题标题】:Adding constraint to css file for a QWidget将约束添加到 QWidget 的 css 文件
【发布时间】:2015-05-06 08:24:07
【问题描述】:

我在我的 Qt Projets 中使用了一个 css 文件和 Visual Studio 2010。

main.cpp:

QApplication app(argc, argv);
// Mise en place du style CSS
QFile File("Resources/style.css");
File.open(QFile::ReadOnly);
QString styleSheet = File.readAll();
File.close();
app.setStyleSheet(styleSheet);

我的 css 文件的一部分:

QWidget#contenuDescription {
    background-color: rgb(0, 150, 255);
    border: 2px solid rgb(0, 0, 255);
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    border-bottom-right-radius: 10px;
    border-bottom-left-radius: 10px;
}

QLabel#nom1 {
    background-color: rgb(0, 150, 255);
    font-size: 36px;
}

QLabel#nom2 {
    background-color: rgb(0, 150, 255);
    font-size: 24px;
}

QLabel#nom3 {
    background-color: rgb(0, 150, 255);
    font-size: 20px;
}

bool m_changeColor == true 时,我想将颜色更改为我的 QLabel:nom1、nom2 和 nom3。

我知道如果我们想在鼠标位于 QLabel 上时更改样式表,我们可以使用::hover。我的问题是否存在这样的问题?

提前感谢您的回答。

【问题讨论】:

  • 当你想改变颜色时,难道不能简单地向 css 添加更多类并更改标签的类吗?

标签: c++ css qt


【解决方案1】:

你需要使用属性:

Q_PROPERTY(bool changeColor ...)

Os 设置属性dynamically:

nom1Label->setProperty("changeColor", true);

然后在 CSS 中:

QLabel#nom1[changeColor="true"] {
    ...
}

另请注意:

警告:如果 Qt 属性的值在样式表之后发生变化 已设置,可能需要强制样式表 重新计算。实现此目的的一种方法是取消设置样式表并 重新设置。

【讨论】:

  • 这正是我想要的。现在我不知道如何取消设置样式表,因为我的样式表是为 QA​​pplication 设置的,但我会找到一个解决方案。非常感谢 svlasov。
  • 你可以设置一个空的样式表像app.setStyleSheet("");
  • QApplication 在我的 main.cpp 中,我在不同的类中使用样式表 ...
  • 所以我这样做了:qApp->setStyleSheet(""); m_contenu->setProperty("couleurFond", 0); QFile File("Resources/style.css"); File.open(QFile::ReadOnly); QString styleSheet = File.readAll(); File.close(); qApp->setStyleSheet(styleSheet);你不认为这不是一个太“硬”的方式吗?
  • 只有在更改属性时才需要重置(取消设置并再次设置)样式表,但可能不需要。此外,无需在第一时间设置空样式表,当应用程序启动时它没有样式表(除非您使用-stylesheet 命令行选项)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
  • 2014-12-05
  • 1970-01-01
  • 2018-01-24
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多