Jepessen
Jepessen

Reputation: 12415

Set stylesheet for private member of a QWidget

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?

Upvotes: 0

Views: 183

Answers (2)

thuga
thuga

Reputation: 12931

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{...}");

Upvotes: 3

shahina
shahina

Reputation: 128

 m_button->setStyleSheet(m_button->styleSheet().append("background-color: rgb(9, 91, 255);"));

This will set the background color only for m_button

Upvotes: 1

Related Questions