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.4k 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 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
                • T tushu

                  @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 Offline
                  M Offline
                  mpergand
                  wrote on last edited by
                  #21

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

                  if they are full filling some criteria, then I set color on them.
                  Why that color is not visible ?

                  fill your form first:
                  painter->fillPath(path, yourColor);

                  T 1 Reply Last reply
                  1
                  • 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
                    #22

                    @JoeCFD
                    If I add below line in paint() , then I get my 3 rectangle ( PD_blue, PD_green, PD_yellow) with thier respective color.

                    QGraphicsRectItem::paint(painter, &copied_option, widget);
                    

                    BUt output I get like this :
                    423d4ef8-f783-4648-9fbe-61a8932099c7-image.png

                    Can you see the corners ? There is a round shape but it is getting overlapped.

                    What is the issue with this line ?

                    QGraphicsRectItem::paint(painter, &copied_option, widget);
                    

                    With this I get color of remaining rectangle but could not see round shape at edge.

                    JoeCFDJ 1 Reply Last reply
                    0
                    • T tushu

                      @JoeCFD
                      If I add below line in paint() , then I get my 3 rectangle ( PD_blue, PD_green, PD_yellow) with thier respective color.

                      QGraphicsRectItem::paint(painter, &copied_option, widget);
                      

                      BUt output I get like this :
                      423d4ef8-f783-4648-9fbe-61a8932099c7-image.png

                      Can you see the corners ? There is a round shape but it is getting overlapped.

                      What is the issue with this line ?

                      QGraphicsRectItem::paint(painter, &copied_option, widget);
                      

                      With this I get color of remaining rectangle but could not see round shape at edge.

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

                      @tushu QGraphicsRectItem::paint(painter, &copied_option, widget); is to call the parent to paint the rect without round corners. Look at the example and this call is not needed.

                      T 1 Reply Last reply
                      1
                      • JoeCFDJ JoeCFD

                        @tushu QGraphicsRectItem::paint(painter, &copied_option, widget); is to call the parent to paint the rect without round corners. Look at the example and this call is not needed.

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

                        @JoeCFD
                        You are correct.
                        But now problem is, after creating rectangle, for some of the rectangle, I am setting some colour with

                        SetBrush()
                        

                        And the color applied with above method, is not visible to the rectangle. They are appearing colorless.

                        Can we use paint() parameter in this case ?

                        paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                        

                        Or is there any other way to solve this issue ?

                        JoeCFDJ 1 Reply Last reply
                        0
                        • T tushu

                          @JoeCFD
                          You are correct.
                          But now problem is, after creating rectangle, for some of the rectangle, I am setting some colour with

                          SetBrush()
                          

                          And the color applied with above method, is not visible to the rectangle. They are appearing colorless.

                          Can we use paint() parameter in this case ?

                          paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                          

                          Or is there any other way to solve this issue ?

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

                          @tushu can you show your full code of this func?

                          T 1 Reply Last reply
                          0
                          • JoeCFDJ JoeCFD

                            @tushu can you show your full code of this func?

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

                            @JoeCFD

                            myRect.h

                            class myRect : public QGraphicsRectItem 
                            {
                                  explicit myRect(QRectF &rectPoints, QGraphicsScene *_scene, QGraphicsItem *parent = nullptr) : QGraphicsRectItem(rectPoints, parent),  _scene(_scene)
                                {}
                            }
                            

                            myRect.cpp

                            void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                            {
                                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(), 0, Qt::SolidLine));
                                }
                                else
                                     painter->setPen(Qt::blue);
                                painter->drawPath(path);
                                painter->restore();
                            }
                            
                            void myRect::CreateRect(QRectF rect)
                            {
                                this->_polyRect = rect;
                                QPen mPen;
                                mPen.setWidth(1);
                                mPen.setBrush(Qt::blue);
                                this->setPen(mPen);
                                this->setFlag(QGraphicsItem::ItemIsSelectable);
                            }
                            

                            mySymbol.cpp

                            DrawSymbol()
                            {
                               QRectF Rect( co-ordinates );
                                myRect* rect = new myRect(Rect, _scene);
                                 rect->CreateRect(Rect);
                                if( if it is power domain ) 
                                  CreatePD(rect);
                            }
                            
                            CreatePD(myRect* rect)
                            {
                                QColor pdColor{some color };
                                rect->setBrush(pdColor);
                            }
                            

                            Note : If I add following line in paint()

                            QGraphicsRectItem::paint(painter, &copied_option, widget);
                            

                            then I can see my rectangle with color. But gets problem with rounded corner.

                            JoeCFDJ 1 Reply Last reply
                            0
                            • T tushu

                              @JoeCFD

                              myRect.h

                              class myRect : public QGraphicsRectItem 
                              {
                                    explicit myRect(QRectF &rectPoints, QGraphicsScene *_scene, QGraphicsItem *parent = nullptr) : QGraphicsRectItem(rectPoints, parent),  _scene(_scene)
                                  {}
                              }
                              

                              myRect.cpp

                              void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                              {
                                  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(), 0, Qt::SolidLine));
                                  }
                                  else
                                       painter->setPen(Qt::blue);
                                  painter->drawPath(path);
                                  painter->restore();
                              }
                              
                              void myRect::CreateRect(QRectF rect)
                              {
                                  this->_polyRect = rect;
                                  QPen mPen;
                                  mPen.setWidth(1);
                                  mPen.setBrush(Qt::blue);
                                  this->setPen(mPen);
                                  this->setFlag(QGraphicsItem::ItemIsSelectable);
                              }
                              

                              mySymbol.cpp

                              DrawSymbol()
                              {
                                 QRectF Rect( co-ordinates );
                                  myRect* rect = new myRect(Rect, _scene);
                                   rect->CreateRect(Rect);
                                  if( if it is power domain ) 
                                    CreatePD(rect);
                              }
                              
                              CreatePD(myRect* rect)
                              {
                                  QColor pdColor{some color };
                                  rect->setBrush(pdColor);
                              }
                              

                              Note : If I add following line in paint()

                              QGraphicsRectItem::paint(painter, &copied_option, widget);
                              

                              then I can see my rectangle with color. But gets problem with rounded corner.

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

                              @tushu Check the example and @mpergand's reply. You do not have
                              painter->fillPath(path, yourColor);
                              Try to set yourColor = Qt::green to see what you get.

                              T 2 Replies Last reply
                              1
                              • JoeCFDJ JoeCFD

                                @tushu Check the example and @mpergand's reply. You do not have
                                painter->fillPath(path, yourColor);
                                Try to set yourColor = Qt::green to see what you get.

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

                                @JoeCFD
                                Your suggestion worked.
                                Thanks for help !

                                1 Reply Last reply
                                0
                                • M mpergand

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

                                  if they are full filling some criteria, then I set color on them.
                                  Why that color is not visible ?

                                  fill your form first:
                                  painter->fillPath(path, yourColor);

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

                                  @mpergand
                                  Your solution worked.
                                  Thanks for help !

                                  1 Reply Last reply
                                  0
                                  • JoeCFDJ JoeCFD

                                    @tushu Check the example and @mpergand's reply. You do not have
                                    painter->fillPath(path, yourColor);
                                    Try to set yourColor = Qt::green to see what you get.

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

                                    @JoeCFD @mpergand
                                    I am facing another small issue in paint()

                                    When I left click on any rectangular object, it's paint() runs. And it highlights that object perfectly.

                                    But when I right click on object ( I have few Right mouse button options ) , paint() method runs, but it does not highlight that object.

                                    Her is my paint()

                                    void myRect::paint(QPainter *painter, QPainterPath path,
                                                                                const QStyleOptionGraphicsItem *option)
                                    {
                                        auto copied_option = *option;
                                        copied_option.state &= ~QStyle::State_Selected;
                                        auto selected = option->state & QStyle::State_Selected;
                                        if(selected)
                                            qDebug()<<"TRUE ";
                                        else
                                            qDebug()<<"FALSE ";
                                        painter->save();
                                        if (selected) {
                                            qDebug()<<"In selected ";
                                            painter->setBrush(Qt::NoBrush);
                                            painter->setPen(QPen(option->palette.windowText(), 1, Qt::SolidLine));
                                            qDebug()<<"Selected completing  ";
                                        } else
                                        {
                                            painter->setPen(Qt::blue);
                                            qDebug()<<"NOT Selected";
                                        }
                                        painter->drawPath(path);
                                        painter->restore();
                                    }
                                    

                                    I checked through qDebug statements, and I found that,

                                    auto selected
                                    

                                    variable has value false on Right click.( i.e. selected = false)

                                    On left click, It works perfectly ( i.e. selected = true )

                                    JoeCFDJ 1 Reply Last reply
                                    0
                                    • T tushu

                                      @JoeCFD @mpergand
                                      I am facing another small issue in paint()

                                      When I left click on any rectangular object, it's paint() runs. And it highlights that object perfectly.

                                      But when I right click on object ( I have few Right mouse button options ) , paint() method runs, but it does not highlight that object.

                                      Her is my paint()

                                      void myRect::paint(QPainter *painter, QPainterPath path,
                                                                                  const QStyleOptionGraphicsItem *option)
                                      {
                                          auto copied_option = *option;
                                          copied_option.state &= ~QStyle::State_Selected;
                                          auto selected = option->state & QStyle::State_Selected;
                                          if(selected)
                                              qDebug()<<"TRUE ";
                                          else
                                              qDebug()<<"FALSE ";
                                          painter->save();
                                          if (selected) {
                                              qDebug()<<"In selected ";
                                              painter->setBrush(Qt::NoBrush);
                                              painter->setPen(QPen(option->palette.windowText(), 1, Qt::SolidLine));
                                              qDebug()<<"Selected completing  ";
                                          } else
                                          {
                                              painter->setPen(Qt::blue);
                                              qDebug()<<"NOT Selected";
                                          }
                                          painter->drawPath(path);
                                          painter->restore();
                                      }
                                      

                                      I checked through qDebug statements, and I found that,

                                      auto selected
                                      

                                      variable has value false on Right click.( i.e. selected = false)

                                      On left click, It works perfectly ( i.e. selected = true )

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

                                      @tushu That means you do not use right click to select your widget. I do not know if it is a good idea to add right click to this feature. If you want to add it, you have to override mouse event to catch mouse right click and then set selected to be true.

                                      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