【问题标题】:How to add a CSS Style Class to a QWidget如何将 CSS 样式类添加到 QWidget
【发布时间】:2017-02-07 17:29:07
【问题描述】:

在 JavaFx 中,我可以轻松添加 CSS 样式类,如下所示:

Scene scene = new Scene(new Group(), 500, 400);
scene.getStylesheets().add("path/stylesheet.css");

......

Label label = new Label("Cool Looking Styled Label");
label.getStyleClass().add("my-label-style");

css

.my-label-style {
    -fx-font: 16px "Serif";
    -fx-padding: 10;
    -fx-background-color: #CCFF99;
}

我如何才能将样式类添加到 QWidget,例如QLabel

【问题讨论】:

  • 我使用Label->setStyleSheet("style sheet goes here"); 然后Label->ensurePolished(); 然后Label->update();

标签: c++ css qt qwidget qtstylesheets


【解决方案1】:

方法在QWidget基类中;它是 QWidget::setStyleSheet。

【讨论】:

    【解决方案2】:
    label->setProperty("class", "my-label-style");
    

    然后在 CSS 中,您可以通过以下方式正常调用它:

    .my-label-style {
      [..]
    }
    

    【讨论】:

      【解决方案3】:

      你有两种方法可以做到这一点

      编码

      • widget.setStyleSheet("css 代码");

      表单用户界面

      • 右键单击小部件
      • 选择更改样式表
      • 会出现一个对话框,您需要在其中编写 css 代码

      但不支持所有的css代码,你需要查看样式表的文档

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-24
        • 2016-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多