[Solved]Remember mouse track Qt



  • I am trying to draw random figures (airfoils eventually) by mouse movement in qt. At this point i am able to track my mouse and a dot appears when the mouse is clicked. However, i want the entire track i follow with my mouse button clicked to be drawn. I thought the mousecoordinates should be saved somewhere but i dont manage to do that. What is the simplest way to draw random figures?
    This is my code up to now. @
    #include "dewidget.h"

    DeWidget::DeWidget(QWidget parent)
    : QGLWidget(QGLFormat(/
    Additional format options */), parent)
    {
    }

    DeWidget::~DeWidget()
    {
    }

    QSize DeWidget::sizeHint() const
    {
    return QSize(840, 480);
    }
    void DeWidget::mouseMoveEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton) {
    setMouseTracking(true);
    }

     a = event->x();
    
     b = event->y();
     update();
    qDebug() << a<<b;
    

    }

    void DeWidget::paintEvent(QPaintEvent *)
    {
    setAttribute(Qt::WA_PaintOnScreen);
    QPainter painter(this);
    QPen linepen(Qt::red);
    linepen.setCapStyle(Qt::RoundCap);
    linepen.setWidth(30);
    painter.setRenderHint(QPainter::Antialiasing,true);
    painter.setPen(linepen);

    painter.drawPoint(a,b);

    }@
    and

    @#include <QWidget>
    #include <QGLWidget>

    #include <QMouseEvent>
    #include <QPainter>

    #include <QGraphicsView>

    class DeWidget : public QGLWidget
    {
    Q_OBJECT

    public:
    DeWidget(QWidget *parent = 0);
    ~DeWidget();
    QSize sizeHint() const;

    protected:
    void mouseMoveEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *);

    private:

    float a;
    float b;
    

    };

    #endif // DEWIDGET_H@



  • I also need these coordinates to later put in a CFD solver.



  • Use QPolygon. In each Mouse move, append the current mouse coordinate. In paint, you can use painter->drawPolyline to paint the whole path.



  • I tried putting my mouse coordinates in an array but it doesnt seem to work. It does not draw anything. What is a convenient way to store mouse coordinates?
    @array1[i]= event->x()
    array2[i] = event ->y()@
    and then i tried drawing it with a loop..:
    @ for (int q=0;q<1000;q++){
    QPolygon polygon;
    polygon << QPoint(array1[q], array2[q])
    painter.drawPolygon(polygon);}@



  • First, I didn't see that you use QGLWidget. I don't know about this, so I cannot tell you why it doesn't draw.

    Second, from what I understood, you need drawPolyline, not drawPolygon.

    Also, the drawPolyline should not be inside the loop, but after the loop.

    Finally, why do you store the coordinates in a separate array, and not into the QPolygon directly? QPolygon is just an array of points, so it should be just what you need.



  • It works! Thank you very much for your help!



  • You are welcome!

    Don't forget to edit your original post, and put "[Solved]" in the thread title.


Log in to reply
 

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