Get margins of QPushButton



  • Is there any way to get margins settled via QSS from code?

    QSS:

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

    Code:

    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:
    alt text


  • Moderators

    @goldstar2154
    thats a really strange issue. Maybe also a bug?!
    The margin property is supported for the QPushButton widget (see here)
    But for QPushButton it indeed returns 0 in all cases, even the margin actually gets applied. See the following example:
    For QFrame:

    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)
    

    Same for QPushButton:

    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)
    

    So the behavior is inconsistent. So this would be a interesting question for the dev mailing list or even worth a bugreport IMO.

    Although you wouldn't receive a margins value of 5 but of 6 (incl. 1px border width). See box model



  • Shoud i open bug?) Have developers read this forum?


  • Moderators

    @goldstar2154 said in Get margins of QPushButton:

    Shoud i open bug?
    Either open a bug or write to the dev mailing list as i wrote.

    Have developers read this forum?

    no this forum isn't read by the developers. The dev mailing list is.



  • you can call QPushButton::styleSheet function to get margin from string, why not


  • Moderators

    @zhangsi said in Get margins of QPushButton:

    you can call QPushButton::styleSheet function to get margin from string, why not

    if it just would be that easy...



  • @goldstar2154 , @raven-worx Have you found how to solve this problem with qpushbutton margins in paintevent?


  • Moderators

    @CppHamster
    you can try the QStyle way:

    void QDelayPushButton::paintEvent(QPaintEvent* event)
    {
          QPushButton::paintEvent(event);
    
          QStyleOptionButton option;
          this->initStyleOption(&option);
          QRect contentsRect = this->style()->subElementRect( QStyle::SE_PushButtonLayoutItem, &option, this );
          ...
    }
    


  • @raven-worx Thank you! But still does not work



  • @raven-worx finally, i've got the contents size by using your advice:
    1: QRect contentsRect1 = this->style()->subElementRect( QStyle::SE_PushButtonLayoutItem, &option, this );
    2: QRect contentsRect2 = this->style()->subElementRect( QStyle::SE_PushButtonContents, &option, this );
    3: QRect contentsRect3 = this->style()->subElementRect( QStyle::SE_PushButtonFocusRect, &option, this );

    qDebug() with QPushbutton properties:
    width: 134
    height: 45
    .....
    QMargins(0, 0, 0, 0) // <- QMargins margins = this->contentsMargins();
    .....
    1: QRect(0,0 0x0)
    2: QRect(1,1 132x43)
    3: QRect(1,1 132x43)

    The difference is border width.



  • @goldstar2154 if the code is correct, you can mark this question as "solved"



  • @CppHamster this code is correct technically, but not logically. If we have contentsMargins() method it shoud work...
    P.S. i've marked question as solved, but still think is a bug
    P.P.S. and thanks for solution, of course)



  • @goldstar2154 Totally agree with you - there is a obvious bug. But thanks to @raven-worx 's code we've got a way to get a result at least. And "solved" will help in web search:)
    Thank you very much for this question:)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.