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. How to paint rectangle around a button considering Padding and margins
QtWS25 Last Chance

How to paint rectangle around a button considering Padding and margins

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.3k Views
  • 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.
  • DonCoderD Offline
    DonCoderD Offline
    DonCoder
    wrote on last edited by DonCoder
    #1

    QPushButton {
    color: #333;
    border: 2px solid #555;
    border-radius: 20px;
    border-style: outset;
    padding: 15px;
    margin-left:10px
    margin-right:10px
    }

    Now there is a padding and margin has been set on my pushbutton. How to draw a rectangle around the pushbutton after applying the margins and padding. So i can visualize how my button is occupying space.

    The reason i am asking this question is in my application i am using different styling for different ui elements. when i am placing it in any layouts the vertical and horizontal gaps are uneven. I want to identify why the uneven spaces are coming into picture

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Do you mean you want to draw on top of the pushbutton to show the actual size?
      like
      alt text

      we cannot draw outside the button.

      DonCoderD 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Do you mean you want to draw on top of the pushbutton to show the actual size?
        like
        alt text

        we cannot draw outside the button.

        DonCoderD Offline
        DonCoderD Offline
        DonCoder
        wrote on last edited by
        #3

        @mrjj Consider my custom widget has a layout and push button is one of the item in the layout. Now in the CUstomWidget paint function, Can i draw that rectangle by measuring the button geometry and calculating its margins and padding ?

        mrjjM 1 Reply Last reply
        0
        • DonCoderD DonCoder

          @mrjj Consider my custom widget has a layout and push button is one of the item in the layout. Now in the CUstomWidget paint function, Can i draw that rectangle by measuring the button geometry and calculating its margins and padding ?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @DonCoder

          Hi
          Sadly we cannot really read any values set in the stylesheet so
          you cant visualize it, if that is the goal.
          We can only show the true geometry (the red frame).

          1 Reply Last reply
          0
          • DonCoderD Offline
            DonCoderD Offline
            DonCoder
            wrote on last edited by
            #5

            @mrjj said in How to paint rectangle around a button considering Padding and margins:

            We can only show the true geometry (the red frame).

            How can we show the true geometry ?? does the geometry() interface provides it ?

            mrjjM 1 Reply Last reply
            0
            • DonCoderD DonCoder

              @mrjj said in How to paint rectangle around a button considering Padding and margins:

              We can only show the true geometry (the red frame).

              How can we show the true geometry ?? does the geometry() interface provides it ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @DonCoder

              Well the function
              https://doc.qt.io/qt-5/qwidget.html#rect-prop
              Tell its client area
              geometry() tells location within the Parent but for widgets (non window)
              also the size.

              What i did with the sample was to subclass QPushButton

              #include <QPushButton>
              #include <QPainter>
              
              class VisButton : public QPushButton
              {
                  Q_OBJECT
              public:
                  explicit VisButton(QWidget *parent = nullptr) : QPushButton(parent) {  
                  }
              
              protected:
                  virtual void paintEvent(QPaintEvent *event) override
                  {
                      QPushButton::paintEvent(event); // paint as normal
                      
                      QPainter p(this);
                      p.setPen(Qt::red);
                      p.drawRect(0,0, width()-1, height()-1); // paint frame.
                      
                  }
              };
              
              #endif // VISBUTTON_H
              
              

              and then promote it
              alt text

              Then it draws itself according to the stylesheet and we "overlay"
              a red frame.

              Not 100% sure, its what you want but it will show how the margins is in relation to the
              Widgets area

              1 Reply Last reply
              1
              • DonCoderD Offline
                DonCoderD Offline
                DonCoder
                wrote on last edited by
                #7

                Thanks... I ll try this

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  You might also want to check KDAB's GammaRay to inspect your application.

                  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