Search code examples
c++qtprivateqstylesheet

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?


Solution

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