Draw an circle from its center.



  • 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.


  • Qt Champions 2017

    Hi
    Do you mean like animated ?



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



  • @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);
    }
    


  • 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);
    }
    


  • @J.Hilk

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.