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. [Solved]Remember mouse track Qt

[Solved]Remember mouse track Qt

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.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.
  • B Offline
    B Offline
    BobMarly
    wrote on last edited by
    #1

    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@

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BobMarly
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BobMarly
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BobMarly
              wrote on last edited by
              #6

              It works! Thank you very much for your help!

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                You are welcome!

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

                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