Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get margins of QPushButton
Forum Updated to NodeBB v4.3 + New Features

Get margins of QPushButton

Scheduled Pinned Locked Moved Solved General and Desktop
qtwidgetsqss
13 Posts 4 Posters 11.9k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G goldstar2154
    25 Aug 2017, 09:23

    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

    R Offline
    R Offline
    raven-worx
    Moderators
    wrote on 25 Aug 2017, 09:56 last edited by
    #2

    @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

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goldstar2154
      wrote on 25 Aug 2017, 10:29 last edited by
      #3

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

      R 1 Reply Last reply 25 Aug 2017, 10:38
      0
      • G goldstar2154
        25 Aug 2017, 10:29

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

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 25 Aug 2017, 10:38 last edited by
        #4

        @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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zhangsi
          wrote on 30 Aug 2017, 03:02 last edited by
          #5

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

          R 1 Reply Last reply 30 Aug 2017, 06:59
          0
          • Z zhangsi
            30 Aug 2017, 03:02

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

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 30 Aug 2017, 06:59 last edited by
            #6

            @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...

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            2
            • C Offline
              C Offline
              CppHamster
              wrote on 13 Sept 2017, 08:11 last edited by
              #7

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

              R 1 Reply Last reply 13 Sept 2017, 08:17
              0
              • C CppHamster
                13 Sept 2017, 08:11

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

                R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 13 Sept 2017, 08:17 last edited by raven-worx
                #8

                @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 );
                      ...
                }
                

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                C 2 Replies Last reply 13 Sept 2017, 15:05
                1
                • R raven-worx
                  13 Sept 2017, 08:17

                  @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 );
                        ...
                  }
                  
                  C Offline
                  C Offline
                  CppHamster
                  wrote on 13 Sept 2017, 15:05 last edited by
                  #9

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

                  1 Reply Last reply
                  0
                  • R raven-worx
                    13 Sept 2017, 08:17

                    @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 );
                          ...
                    }
                    
                    C Offline
                    C Offline
                    CppHamster
                    wrote on 13 Sept 2017, 15:45 last edited by
                    #10

                    @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.

                    1 Reply Last reply
                    3
                    • C Offline
                      C Offline
                      CppHamster
                      wrote on 20 Sept 2017, 10:44 last edited by
                      #11

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

                      G 1 Reply Last reply 20 Sept 2017, 13:21
                      0
                      • C CppHamster
                        20 Sept 2017, 10:44

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

                        G Offline
                        G Offline
                        goldstar2154
                        wrote on 20 Sept 2017, 13:21 last edited by goldstar2154
                        #12

                        @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)

                        C 1 Reply Last reply 20 Sept 2017, 17:10
                        1
                        • G goldstar2154
                          20 Sept 2017, 13:21

                          @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)

                          C Offline
                          C Offline
                          CppHamster
                          wrote on 20 Sept 2017, 17:10 last edited by CppHamster
                          #13

                          @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:)

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved