问题
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!
回答1:
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);
}
来源:https://stackoverflow.com/questions/21020180/clicked-signal-for-qwidget-breaks-stylesheets