我想知道下面的样式表语句是否正确或错误是什么。即使我设置它也不会改变UI。
QWidget#itemScrollAreaWidgetContents PosButton[pressed = "false"] QFrame#baseWidget
{
border: 1px solid black;
border-top-width: 0;
border-left-width: 0;
border-style: outset;
}
QWidget#itemScrollAreaWidgetContents PosButton[pressed = "true"] QFrame#baseWidget
{
border: 1px solid black;
border-top-width: 0;
border-left-width: 0;
border-style: outset;
}
我的自定义窗口小部件PosButton具有属性集
Q_PROPERTY(QString pressed READ pressed WRITE setPressed)
我在mouseclick
上更改了属性void PosButton::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() << Q_FUNC_INFO << "Invoked";
if(rect().contains(event->pos()))
{
qDebug() << Q_FUNC_INFO << "Emit click";
emit clicked(this->objectName());
}
setPressed("false");
style()->unpolish(this);
style()->polish(this);
qDebug() << Q_FUNC_INFO << "Exits";
return;
}
void PosButton::mousePressEvent(QMouseEvent *event)
{
qDebug() << Q_FUNC_INFO << "Invoked";
Q_UNUSED(event);
setPressed("true");
style()->unpolish(this);
style()->polish(this);
qDebug() << Q_FUNC_INFO << "Exits";
return;
}
另外,我尝试设置样式表。它会受到影响,但需要一秒钟才能在UI中更新。即,我需要按住我的小部件一秒钟才能显示更改
void PosButton::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() << Q_FUNC_INFO << "Invoked";
if(rect().contains(event->pos()))
{
qDebug() << Q_FUNC_INFO << "Emit click";
emit clicked(this->objectName());
}
m_pBaseWidget->setStyleSheet("QFrame#baseWidget {"
"border: 1px solid black;"
"border-top-width: 0;"
"border-left-width: 0;"
"border-style: outset;}"
);
qDebug() << Q_FUNC_INFO << "Exits";
return;
}
void PosButton::mousePressEvent(QMouseEvent *event)
{
qDebug() << Q_FUNC_INFO << "Invoked";
Q_UNUSED(event);
m_pBaseWidget->setStyleSheet("QFrame#baseWidget {"
"border: 2px solid orange;"
"border-top-width: 0;"
"border-left-width: 0;"
"border-style: outset;}"
);
qDebug() << Q_FUNC_INFO << "Exits";
return;
}
答案 0 :(得分:0)
AFAIK Qt Style Sheets
只有在您实现paintEvent时才能使用直接QWidget
子类:
void PosButton::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
我还建议您为bool
属性使用pressed
类型,如下所示:
Q_PROPERTY(bool pressed READ pressed WRITE setPressed)
并以与您已经完成相同的方式引用该属性(不确定在=
之间和之后是否可以留空格):
QWidget#itemScrollAreaWidgetContents PosButton[pressed="false"] QFrame#baseWidget