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. Draw an circle from its center.
Forum Updated to NodeBB v4.3 + New Features

Draw an circle from its center.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 10.9k 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.
  • V Offline
    V Offline
    vlibertiaux
    wrote on last edited by
    #1

    Hi everyone,

    I am brand new to QT and I have the following question:

    I would like to know how it is possible to draw a circle from its center. I am working with a class which is basically a modified QRectF and let's say that it is used to draw an ellipse on an image.
    I have a boolean that indicates if the circle is to be drawn from its center and I tried to modify the paint method inherited by the class accordingly but with little to no success so far.

    Here is the code I wrote, if it can be of any interest:

    void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(widget);
        painter->setPen(m_pen);
        painter->setBrush(m_brush);
        QPointF center = this->rect().center();
        qreal radius = abs(this->rect().right() - this->rect().left())/2.;
        QPointF newTopLeft(center);
        newTopLeft.setX(newTopLeft.x() - radius);
        newTopLeft.setY(newTopLeft.y() - radius);
        QPointF newBottomRight(center);
        newBottomRight.setX(newBottomRight.x() + radius);
        newBottomRight.setY(newBottomRight.y() + radius);
    
        QRectF modified(newTopLeft, newBottomRight);
        QPointF newCenter = mapFromItem(this, center);
        modified.moveCenter(newCenter);
        this->setRect(modified);
        if(m_ellipse && m_fromCenter)
        {
            painter->save();
            painter->drawEllipse(this->rect().center(), radius, radius);
            painter->restore();
        }
        if(m_ellipse)
        {
            painter->drawEllipse(this->rect());
        }
    }
    

    Can someone help me ?

    Thank you very much in advance,
    V.

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

      Hi
      Do you mean like animated ?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vlibertiaux
        wrote on last edited by
        #3

        Yes.
        The radius would change as the user would move the mouse away or towards the center point.

        J.HilkJ 1 Reply Last reply
        0
        • V vlibertiaux

          Yes.
          The radius would change as the user would move the mouse away or towards the center point.

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @vlibertiaux do you mean something like this?

          //main.cpp
          
          #include "widget.h"
          #include <QApplication>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              Widget w;
              w.show();
          
              return a.exec();
          }
          
          //Widget.h
          #ifndef WIDGET_H
          #define WIDGET_H
          
          #include <QWidget>
          
          class Widget : public QWidget
          {
              Q_OBJECT
          
          public:
              Widget(QWidget *parent = 0);
              ~Widget();
          
          protected:
              virtual void mouseMoveEvent(QMouseEvent *event)Q_DECL_OVERRIDE;
              virtual void paintEvent(QPaintEvent *event)Q_DECL_OVERRIDE;
          
              QPoint m_mousPos;
          };
          
          #endif // WIDGET_H
          
          //widget.cpp
          #include "widget.h"
          #include <QMouseEvent>
          #include <QPainter>
          
          Widget::Widget(QWidget *parent)
              : QWidget(parent)
          {
              this->resize(500,500);
              this->setMouseTracking(true);
          }
          
          Widget::~Widget()
          {
          
          }
          
          void Widget::mouseMoveEvent(QMouseEvent *event)
          {
              m_mousPos = event->pos();
              update();
          }
          
          void Widget::paintEvent(QPaintEvent *event)
          {
              QWidget::paintEvent(event);
          
              QPainter p(this);
              QRect r(0,0,m_mousPos.x(),m_mousPos.y());
              r.moveCenter(QPoint(width()/2,height()/2));
          
              p.drawEllipse(r);
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            Or if you want it to be more atuned to your Mous-Position:

            void Widget::paintEvent(QPaintEvent *event)
            {
                QWidget::paintEvent(event);
            
                QPainter p(this);
                int w = m_mousPos.x() < width()/2 ? (width()/2 -m_mousPos.x())*2 : (m_mousPos.x()- width()/2)*2;
                int h = m_mousPos.y() < height()/2 ? (height()/2 -m_mousPos.y())*2 : (m_mousPos.y()-height()/2)*2;
                QRect r(0,0,w, h);
                r.moveCenter(QPoint(width()/2,height()/2));
            
                p.drawEllipse(r);
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            V 1 Reply Last reply
            2
            • J.HilkJ J.Hilk

              Or if you want it to be more atuned to your Mous-Position:

              void Widget::paintEvent(QPaintEvent *event)
              {
                  QWidget::paintEvent(event);
              
                  QPainter p(this);
                  int w = m_mousPos.x() < width()/2 ? (width()/2 -m_mousPos.x())*2 : (m_mousPos.x()- width()/2)*2;
                  int h = m_mousPos.y() < height()/2 ? (height()/2 -m_mousPos.y())*2 : (m_mousPos.y()-height()/2)*2;
                  QRect r(0,0,w, h);
                  r.moveCenter(QPoint(width()/2,height()/2));
              
                  p.drawEllipse(r);
              }
              
              V Offline
              V Offline
              vlibertiaux
              wrote on last edited by
              #6

              @J.Hilk

              This is exactly what I was looking for, thank you very much !

              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