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 draw rectangle with all corners rounded ?
Forum Updated to NodeBB v4.3 + New Features

How to draw rectangle with all corners rounded ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
31 Posts 4 Posters 10.0k Views 1 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.
  • T Offline
    T Offline
    tushu
    wrote on last edited by tushu
    #1

    I am trying to draw rectangle in the QGraphicsScene.
    It is coming like this.

    9c5e4f3f-be58-4362-af3f-57c87f96ee35-image.png

    But I want all its 4 corner rounded.
    How to do that ?

    I have created rectangle this way:

    myRect.h

    class myRect: public QGraphicsRectItem {
    explicit myRect(QRectF &rectPoints,  QGraphicsScene *_scene,
                                  QGraphicsItem *parent = nullptr)
            : QGraphicsRectItem(rectPoints, parent),  _scene(_scene)
        {}
    CreateRect(QRectF);
        void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    
    }
    

    myRect.cpp

    void myRect::CreateRect(QRectF rect)
    {
        QPen mPen;
        mPen.setWidth(1);
        mPen.setBrush(Qt::blue);
        this->setPen(mPen);
        this->setFlag(QGraphicsItem::ItemIsSelectable);
    }
    
    void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        auto copied_option = *option;
        copied_option.state &= ~QStyle::State_Selected;
        auto selected = option->state & QStyle::State_Selected;
        QGraphicsRectItem::paint(painter, &copied_option, widget);
        if (selected) {
            painter->save();
            painter->setBrush(Qt::NoBrush);
            painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine));
            painter->drawPath(shape());
            painter->restore();
        }
    }
    

    myView.cpp

    QRectF Rect( some co-ordinates);
    myRect* rect = new myRect(Rect, _scene);
    rect->CreateRect(Rect);
    
    JoeCFDJ 1 Reply Last reply
    0
    • T tushu

      I am trying to draw rectangle in the QGraphicsScene.
      It is coming like this.

      9c5e4f3f-be58-4362-af3f-57c87f96ee35-image.png

      But I want all its 4 corner rounded.
      How to do that ?

      I have created rectangle this way:

      myRect.h

      class myRect: public QGraphicsRectItem {
      explicit myRect(QRectF &rectPoints,  QGraphicsScene *_scene,
                                    QGraphicsItem *parent = nullptr)
              : QGraphicsRectItem(rectPoints, parent),  _scene(_scene)
          {}
      CreateRect(QRectF);
          void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
      
      }
      

      myRect.cpp

      void myRect::CreateRect(QRectF rect)
      {
          QPen mPen;
          mPen.setWidth(1);
          mPen.setBrush(Qt::blue);
          this->setPen(mPen);
          this->setFlag(QGraphicsItem::ItemIsSelectable);
      }
      
      void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
      {
          auto copied_option = *option;
          copied_option.state &= ~QStyle::State_Selected;
          auto selected = option->state & QStyle::State_Selected;
          QGraphicsRectItem::paint(painter, &copied_option, widget);
          if (selected) {
              painter->save();
              painter->setBrush(Qt::NoBrush);
              painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine));
              painter->drawPath(shape());
              painter->restore();
          }
      }
      

      myView.cpp

      QRectF Rect( some co-ordinates);
      myRect* rect = new myRect(Rect, _scene);
      rect->CreateRect(Rect);
      
      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      @tushu https://stackoverflow.com/questions/29196610/qt-drawing-a-filled-rounded-rectangle-with-border

      T 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @tushu https://stackoverflow.com/questions/29196610/qt-drawing-a-filled-rounded-rectangle-with-border

        T Offline
        T Offline
        tushu
        wrote on last edited by
        #3

        @JoeCFD Thanks for your reply.
        I had seen that link but did not get how to use

        addRoundedRect()
        

        Because it is called by QPainterPath. And I do not have QPainterPath.

        And I am drawing rectangle through constructor.
        How to use QpainterPath in such case ?

        JonBJ JoeCFDJ 2 Replies Last reply
        0
        • T tushu

          @JoeCFD Thanks for your reply.
          I had seen that link but did not get how to use

          addRoundedRect()
          

          Because it is called by QPainterPath. And I do not have QPainterPath.

          And I am drawing rectangle through constructor.
          How to use QpainterPath in such case ?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @tushu said in How to draw rectangle with all corners rounded ?:

          Because it is called by QPainterPath. And I do not have QPainterPath.

          Both the examples in the link show they create a QPainterPath as a local variable. The second one shows a complete paintEvent() override.

          And I am drawing rectangle through constructor.

          You can't. Drawing is done in paintEvent() as shown.

          EDIT I had not looked closely enough at your situation with graphics as opposed to the SO link for widgets. My bad.

          T 1 Reply Last reply
          0
          • JonBJ JonB

            @tushu said in How to draw rectangle with all corners rounded ?:

            Because it is called by QPainterPath. And I do not have QPainterPath.

            Both the examples in the link show they create a QPainterPath as a local variable. The second one shows a complete paintEvent() override.

            And I am drawing rectangle through constructor.

            You can't. Drawing is done in paintEvent() as shown.

            EDIT I had not looked closely enough at your situation with graphics as opposed to the SO link for widgets. My bad.

            T Offline
            T Offline
            tushu
            wrote on last edited by tushu
            #5

            @JonB Thanks for reply.
            I tried to create QPainterPath variable in constructor where I have rectangle co-ordinates. But I could not succeed.

            So another thought was to create in PaintEvent()
            But in my paintEvent() , how will I get co-ordinates of rectangle ?
            Because , it is virtual method, and does not accept co-ordinates in parameter.

            1 Reply Last reply
            0
            • T tushu

              @JoeCFD Thanks for your reply.
              I had seen that link but did not get how to use

              addRoundedRect()
              

              Because it is called by QPainterPath. And I do not have QPainterPath.

              And I am drawing rectangle through constructor.
              How to use QpainterPath in such case ?

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #6

              @tushu You are not drawing it in the constructor. Your code overrides paint func. Therefore, the rect is drawn in paint(). Since the code in the link needs only painter and you have it in the paint(), I guess you can do the same thing. There is no paintEvent() for QGraphicsRectItem.

              T 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @tushu You are not drawing it in the constructor. Your code overrides paint func. Therefore, the rect is drawn in paint(). Since the code in the link needs only painter and you have it in the paint(), I guess you can do the same thing. There is no paintEvent() for QGraphicsRectItem.

                T Offline
                T Offline
                tushu
                wrote on last edited by
                #7

                @JoeCFD @JonB

                I have added following code in paint() , but did not get any effect.

                QRectF rect = QRectF(widget->rect());
                QPainterPath path;
                path.addRoundedRect(rect, 10, 10);
                
                JoeCFDJ 1 Reply Last reply
                0
                • T tushu

                  @JoeCFD @JonB

                  I have added following code in paint() , but did not get any effect.

                  QRectF rect = QRectF(widget->rect());
                  QPainterPath path;
                  path.addRoundedRect(rect, 10, 10);
                  
                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #8

                  @tushu add print message or set break point there to check if the func is called. Also show your full code of this func.

                  T 2 Replies Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @tushu add print message or set break point there to check if the func is called. Also show your full code of this func.

                    T Offline
                    T Offline
                    tushu
                    wrote on last edited by tushu
                    #9

                    @JoeCFD
                    my current output
                    90a64852-026d-48f1-9ca9-e6f3137dc051-image.png

                    And code in paint()

                    void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                    {
                        QRectF rect = QRectF(widget->rect());
                        QPainterPath path;
                        painter->drawRoundedRect(rect, 10, 10);
                    auto copied_option = *option;
                        copied_option.state &= ~QStyle::State_Selected;
                        auto selected = option->state & QStyle::State_Selected;
                        QGraphicsRectItem::paint(painter, &copied_option, widget);
                        if (selected) {
                            painter->save();
                            painter->setBrush(Qt::NoBrush);
                            painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine));
                            painter->drawPath(shape());
                            painter->restore();
                        }
                    
                    1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @tushu add print message or set break point there to check if the func is called. Also show your full code of this func.

                      T Offline
                      T Offline
                      tushu
                      wrote on last edited by
                      #10

                      @JoeCFD
                      Some big rectangle is getting drawn.
                      I think that is because of

                      QRectF rect = QRectF(widget->rect());
                      

                      I am taking widget->rect().
                      But I am expecting, every rectangle in the scene should have rounded corner.

                      JoeCFDJ 1 Reply Last reply
                      0
                      • T tushu

                        @JoeCFD
                        Some big rectangle is getting drawn.
                        I think that is because of

                        QRectF rect = QRectF(widget->rect());
                        

                        I am taking widget->rect().
                        But I am expecting, every rectangle in the scene should have rounded corner.

                        JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by JoeCFD
                        #11

                        @tushu

                        Something like this.

                        void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                        {
                            QRectF rect = QRectF(widget->rect());
                            QPainterPath path;
                            path->drawRoundedRect(rect, 10, 10); /* path not painter */
                            auto copied_option = *option;
                            copied_option.state &= ~QStyle::State_Selected;
                            auto selected = option->state & QStyle::State_Selected;
                            //QGraphicsRectItem::paint(painter, &copied_option, widget);  /* you do not call this one  */
                            painter->save();
                            if (selected) {
                                painter->setBrush(Qt::NoBrush);
                                painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine));
                           }
                           else {
                               set pen color;
                           }       
                           painter->drawPath(path); 
                           painter->restore();
                        }
                        
                        T 1 Reply Last reply
                        0
                        • JoeCFDJ JoeCFD

                          @tushu

                          Something like this.

                          void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                          {
                              QRectF rect = QRectF(widget->rect());
                              QPainterPath path;
                              path->drawRoundedRect(rect, 10, 10); /* path not painter */
                              auto copied_option = *option;
                              copied_option.state &= ~QStyle::State_Selected;
                              auto selected = option->state & QStyle::State_Selected;
                              //QGraphicsRectItem::paint(painter, &copied_option, widget);  /* you do not call this one  */
                              painter->save();
                              if (selected) {
                                  painter->setBrush(Qt::NoBrush);
                                  painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine));
                             }
                             else {
                                 set pen color;
                             }       
                             painter->drawPath(path); 
                             painter->restore();
                          }
                          
                          T Offline
                          T Offline
                          tushu
                          wrote on last edited by tushu
                          #12

                          @JoeCFD

                          I have used

                          painter.drawRoundedRect(rect, 10, 10);
                          

                          because QPainterPath does not have drawRoundedRect()

                          current output

                          5ed845a7-d434-4043-9bbd-5f058cdc6dac-image.png

                          Below lines creating difference.

                          //QGraphicsRectItem::paint(painter, &copied_option, widget);  /* you do not call this one  */
                          
                          JoeCFDJ 2 Replies Last reply
                          0
                          • T tushu

                            @JoeCFD

                            I have used

                            painter.drawRoundedRect(rect, 10, 10);
                            

                            because QPainterPath does not have drawRoundedRect()

                            current output

                            5ed845a7-d434-4043-9bbd-5f058cdc6dac-image.png

                            Below lines creating difference.

                            //QGraphicsRectItem::paint(painter, &copied_option, widget);  /* you do not call this one  */
                            
                            JoeCFDJ Offline
                            JoeCFDJ Offline
                            JoeCFD
                            wrote on last edited by JoeCFD
                            #13

                            @tushu

                            void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                            {
                                QRectF rect = QRectF(widget->rect());
                                QPainterPath path;
                                path->addRoundedRect(rect, 10, 10); /* add not draw sorry */
                                auto copied_option = *option;
                                copied_option.state &= ~QStyle::State_Selected;
                                auto selected = option->state & QStyle::State_Selected;
                                //QGraphicsRectItem::paint(painter, &copied_option, widget);  /* drawing is finished here and no need to call parent->paint()  */
                                painter->save();
                                if (selected) {
                                    painter->setBrush(Qt::NoBrush);
                                    painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine));
                               }
                               else {
                                   set pen color;
                               }       
                               painter->drawPath(path); 
                               painter->restore();
                            }
                            
                            T 1 Reply Last reply
                            0
                            • T tushu

                              @JoeCFD

                              I have used

                              painter.drawRoundedRect(rect, 10, 10);
                              

                              because QPainterPath does not have drawRoundedRect()

                              current output

                              5ed845a7-d434-4043-9bbd-5f058cdc6dac-image.png

                              Below lines creating difference.

                              //QGraphicsRectItem::paint(painter, &copied_option, widget);  /* you do not call this one  */
                              
                              JoeCFDJ Offline
                              JoeCFDJ Offline
                              JoeCFD
                              wrote on last edited by
                              #14

                              @tushu You also need to try the example first

                              void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                              {
                              painter->setRenderHint(QPainter::Antialiasing);
                              QPainterPath path;
                              path.addRoundedRect(QRectF(10, 10, 100, 50), 10, 10);
                              QPen pen(Qt::black, 10);
                              painter->setPen(pen);
                              painter->fillPath(path, Qt::red);
                              painter->drawPath(path);
                              }
                              
                              1 Reply Last reply
                              0
                              • JoeCFDJ JoeCFD

                                @tushu

                                void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                                {
                                    QRectF rect = QRectF(widget->rect());
                                    QPainterPath path;
                                    path->addRoundedRect(rect, 10, 10); /* add not draw sorry */
                                    auto copied_option = *option;
                                    copied_option.state &= ~QStyle::State_Selected;
                                    auto selected = option->state & QStyle::State_Selected;
                                    //QGraphicsRectItem::paint(painter, &copied_option, widget);  /* drawing is finished here and no need to call parent->paint()  */
                                    painter->save();
                                    if (selected) {
                                        painter->setBrush(Qt::NoBrush);
                                        painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine));
                                   }
                                   else {
                                       set pen color;
                                   }       
                                   painter->drawPath(path); 
                                   painter->restore();
                                }
                                
                                T Offline
                                T Offline
                                tushu
                                wrote on last edited by
                                #15

                                @JoeCFD
                                For this , getting the same above output.
                                (i.e. No border of any rectangle)

                                JoeCFDJ 1 Reply Last reply
                                0
                                • T tushu

                                  @JoeCFD
                                  For this , getting the same above output.
                                  (i.e. No border of any rectangle)

                                  JoeCFDJ Offline
                                  JoeCFDJ Offline
                                  JoeCFD
                                  wrote on last edited by
                                  #16

                                  @tushu did you try the example?

                                  T 2 Replies Last reply
                                  0
                                  • JoeCFDJ JoeCFD

                                    @tushu did you try the example?

                                    T Offline
                                    T Offline
                                    tushu
                                    wrote on last edited by
                                    #17

                                    @JoeCFD

                                    Example's output

                                    d0017cbd-e8f8-45d1-83f7-bead1be99d3b-image.png

                                    I have just removed

                                    //painter->fillPath(path, Qt::red);
                                    
                                    1 Reply Last reply
                                    0
                                    • JoeCFDJ JoeCFD

                                      @tushu did you try the example?

                                      T Offline
                                      T Offline
                                      tushu
                                      wrote on last edited by tushu
                                      #18

                                      @JoeCFD
                                      Is the below line correct ?

                                      QRectF rect = QRectF(widget->rect());
                                      

                                      because of the above line , it is creating bigger rectangle ?
                                      But bigger rectangle ( outermost ) has curved corner.

                                      JoeCFDJ 1 Reply Last reply
                                      0
                                      • T tushu

                                        @JoeCFD
                                        Is the below line correct ?

                                        QRectF rect = QRectF(widget->rect());
                                        

                                        because of the above line , it is creating bigger rectangle ?
                                        But bigger rectangle ( outermost ) has curved corner.

                                        JoeCFDJ Offline
                                        JoeCFDJ Offline
                                        JoeCFD
                                        wrote on last edited by
                                        #19

                                        @tushu you know the size of your rects, right? I do not know.

                                        T 2 Replies Last reply
                                        0
                                        • JoeCFDJ JoeCFD

                                          @tushu you know the size of your rects, right? I do not know.

                                          T Offline
                                          T Offline
                                          tushu
                                          wrote on last edited by tushu
                                          #20

                                          @JoeCFD
                                          Your rectangle size suggestion worked.
                                          Current output.

                                          296e6f8c-ef10-4ecb-91bb-d7685fd8f2e5-image.png

                                          Now I want to round corner of middle 5 rectangle only ( i.e. sw1,sw2,PD_blue, PD_green,PD_yellow)
                                          But left side rectangle ( small ) also got changed and looking like circle.

                                          I think, I will need to set some flag and will have to decide, which rectangle's corner should be rounded.

                                          But now main concern is PD_blue, PD_green,PD_yellow rectangles color is not visible.

                                          I set their color, after creating rectangle using setBrush(). I do not set their color in paint(). After creating rectangle, if they are full filling some criteria, then I set color on them.

                                          Why that color is not visible ?

                                          Here is my paint()

                                          void guiSchematicRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                                          {
                                              Q_UNUSED(option);
                                              Q_UNUSED(widget);
                                              QRectF rect = QRectF(_polyRect);
                                              QPainterPath path;
                                              path.addRoundedRect(rect, 4, 4);
                                             auto copied_option = *option;
                                              copied_option.state &= ~QStyle::State_Selected;
                                              auto selected = option->state & 
                                             QStyle::State_Selected;
                                              painter->save();
                                              if (selected) {
                                                  painter->setBrush(Qt::NoBrush);
                                                  painter->setPen(QPen(option->palette.windowText(), 1, Qt::SolidLine));
                                             }
                                             else {
                                                 painter->setPen(Qt::blue);
                                             }
                                             painter->drawPath(path);
                                             painter->restore();
                                          }
                                          
                                          M 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