获得QPushButton的边缘

时间:2017-08-25 08:57:42

标签: c++ qt qtstylesheets

有没有办法通过Qt样式表从代码中解决边距?

样式表:

QPushButton {
    background: #E1E1E1;
    border-style: solid;
    border-color: #ADADAD;
    border-width: 1px;
    margin: 5px;
}

代码:

void QDelayPushButton::paintEvent(QPaintEvent* event)
{
    QPushButton::paintEvent(event);
    QPainter painter(this);
    QRect workRect = rect();               // Always return full button rect
    QRect rect2  = contentsRect();         // Always = workRect;
    QMargins margins = contentsMargins();  // Always 0, 0, 0, 0 (no matter what in qss)
    ...
}

example

UPD1: 这是简约的例子:

QFrame b;
b.setStyleSheet( "margin: 10px;" );
b.show();
qDebug() << b.geometry() << b.contentsRect() << b.contentsMargins(); // QRect(640,280 640x480) QRect(10,10 620x460) QMargins(10, 10, 10, 10)

QPushButton b;
b.setStyleSheet( "margin: 10px;" );
b.show();
qDebug() << b.geometry() << b.contentsRect() << b.contentsMargins(); // QRect(640,280 46x35) QRect(0,0 46x35) QMargins(0, 0, 0, 0)

0 个答案:

没有答案