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
Qt 6.11 is out! See what's new in the release blog

Get margins of QPushButton

Scheduled Pinned Locked Moved Solved General and Desktop
qtwidgetsqss
15 Posts 6 Posters 15.0k Views 5 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.
  • Z zhangsi

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

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on 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 last edited by
      #7

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

      raven-worxR 1 Reply Last reply
      0
      • C CppHamster

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

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on 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
        1
        • raven-worxR raven-worx

          @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 last edited by
          #9

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

          1 Reply Last reply
          0
          • raven-worxR raven-worx

            @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 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 last edited by
              #11

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

              G 1 Reply Last reply
              0
              • C CppHamster

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

                G Offline
                G Offline
                goldstar2154
                wrote on 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
                1
                • G goldstar2154

                  @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 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
                  • S Offline
                    S Offline
                    Sordayne
                    wrote last edited by
                    #14

                    This bug seems to have not been fixed in Qt 6.10. Have you found a solution?

                    SGaistS 1 Reply Last reply
                    0
                    • S Sordayne

                      This bug seems to have not been fixed in Qt 6.10. Have you found a solution?

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote last edited by
                      #15

                      @Sordayne said in Get margins of QPushButton:

                      This bug seems to have not been fixed in Qt 6.10. Have you found a solution?

                      Hi and welcome to devnet,

                      In that case, you should check the bug report system and if there's nothing, please open a new report providing a minimal compilable example that shows the issue.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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