【问题标题】:Set stylesheet for private member of a QWidget为 QWidget 的私有成员设置样式表
【发布时间】:2018-03-15 22:04:56
【问题描述】:

我有一个类似以下的课程:

class BigButton : public QWidget {

  Q_OBJECT

public:

  BigButton(QWidget* parent = nullptr);
  virtual ~BigButton() = default;
  void setSvgImagePath(const QString& imagePath);
  void setLabel(const QString& label) override;

private:

  QLabel* m_label;
  QSvgWidget* m_svgImage;
  QPushButton* m_button;
};

我想为我的应用程序创建一个样式表,允许设置私有QPushButton 成员m_button 的一些属性(如背景颜色),但不能设置我的GUI 周围的其他QPushButton

我已经了解了如何设置stylesheet for subclasses,但是我找不到为类的特定私有成员设置样式表的方法。有没有办法实现?

【问题讨论】:

  • 为什么不设置对象名?

标签: c++ qt private qstylesheet


【解决方案1】:

根据 eyllanesc 的建议,将 set an object name 添加到您的按钮,并在样式表中使用 ID selector

m_button->setObjectName("myButton");

widget->setStyleSheet("QPushButton#myButton{...}");

【讨论】:

    【解决方案2】:
     m_button->setStyleSheet(m_button->styleSheet().append("background-color: rgb(9, 91, 255);"));
    

    这将为 m_button 设置背景颜色

    【讨论】:

    • 没错,但我想通过合成来设置。我有一个包含按钮的类,我想为该类设置样式表,以便自动更改按钮。
    猜你喜欢
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2023-03-24
    • 2015-05-03
    • 1970-01-01
    相关资源
    最近更新 更多