Question

I want to connect clicked() signal of QWidget-container to my slot. So I wrote this code:

hpp:

class LinkWidget : public QWidget
{
    Q_OBJECT

public:
    explicit LinkWidget(QWidget * parent = 0 );
    ~LinkWidget();

signals:
    void clicked();

protected:
    void mousePressEvent(QMouseEvent * event ) ;
};

cpp:

LinkWidget::LinkWidget(QWidget * parent) :
    QWidget(parent)
{

}

LinkWidget::~LinkWidget()
{

}

void LinkWidget::mousePressEvent(QMouseEvent * event)
{
    emit clicked();
}

Everything works fine, but I can't assign stylesheet to this widget anymore:

That works:

QWidget * statuspanel = new QWidget(this);
statuspanel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
statuspanel->setMinimumHeight(20);
statuspanel->setMaximumHeight(20);
statuspanel->setStyleSheet("border-bottom: 1px solid rgb(206, 203, 186);"
                           "border-right : 1px solid rgb(206, 203, 186);");

That doesn't:

LinkWidget * statuspanel = new LinkWidget(this);
statuspanel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
statuspanel->setMinimumHeight(20);
statuspanel->setMaximumHeight(20);
statuspanel->setStyleSheet("border-bottom: 1px solid rgb(206, 203, 186);"
                           "border-right : 1px solid rgb(206, 203, 186);");

What am I doing wrong?

Thanks!

Was it helpful?

Solution

In order to stylesheets work for direct QWidget subclasses you should reimplement the paintEvent method:

void LinkWidget::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top