0

I've a class like the following one:

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;
};

I want to create a stylesheet for my application that allows to set some properties (like the background color) of the private QPushButton member m_button, but not other QPushButton around my GUI.

I've seen how to set stylesheet for subclasses, but I cannot find a way to set the stylesheet for the specific private member of a class. Is there a way to achieve it?

Jepessen
  • 11,744
  • 14
  • 82
  • 149

2 Answers2

2

As suggested by eyllanesc, set an object name to your button, and use the ID selector in your stylesheet:

m_button->setObjectName("myButton");

widget->setStyleSheet("QPushButton#myButton{...}");
thuga
  • 12,601
  • 42
  • 52
1
 m_button->setStyleSheet(m_button->styleSheet().append("background-color: rgb(9, 91, 255);"));

This will set the background color only for m_button

shahina
  • 138
  • 8
  • That's correct but I want to set it by composition. I've a class that contains the button, and I want to set the stylesheet for that class, in order to change the button automatically. – Jepessen Mar 16 '18 at 08:12