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] PaintEvent, drawing many dots/points using Array.
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] PaintEvent, drawing many dots/points using Array.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 6.3k 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.
  • M Offline
    M Offline
    MrNoway
    wrote on last edited by
    #1

    Hey, I need little help with my script

    so far I coded a dot, that moves from left to right.
    HEADER
    @class Lines : public QWidget
    {
    Q_OBJECT

    QTimer *timer1;
    

    public:
    Lines(QWidget *parent = 0);
    int timer_interval;

    protected:
    void paintEvent(QPaintEvent *event);
    void drawLines(QPainter *qp);
    public slots:
    void call_repaint();

    };@

    MAIN
    @Lines::Lines(QWidget *parent) : QWidget(parent)
    {

    timer_interval=0;
           timer1=new QTimer(this);
    timer1->setInterval(100);
    timer1->start();
    connect(timer1, SIGNAL(timeout()), this, SLOT(call_repaint()));
    

    }

    void Lines::paintEvent(QPaintEvent *e)
    {
    Q_UNUSED(e);
    QPainter qp(this);
    drawLines(&qp);
    }

    void Lines::call_repaint(){repaint();}
    void Lines::drawLines(QPainter *qp)
    {
    timer_interval++;

    QPen pen(Qt::black, 2, Qt::SolidLine);
    qp->setPen(pen);
    qp->drawPoint(2+timer_interval,50);
    }@

    This is working fine, but I want instead one dot moving, many dots next to each other, so the dots are making a line or any kind of graph.

    So long story short, I need an Array of the Paintevent or something, how to do this?

    e.g.

    first dot/point is on coordinate (10,10)
    next one is on (10,11)
    next one is on (10,12)
    ...

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

      typically, the paint event repaints everything. It could also happen after the window was covered by another window. So if you really want to draw a line, move one end point and leave the starting point where it is.

      If it should be dots, use an array.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MrNoway
        wrote on last edited by
        #3

        @If it should be dots, use an array.@

        -___-
        yea, but how do I do this?

        if I do like this, nothing is drawn

        @ QPainter paint_array[10];

        drawLines(&paint_array[0]);@

        I want to draw a graph/curve, so I need to manipulate every single dot,
        using a line is not an option for me

        1 Reply Last reply
        0
        • T Offline
          T Offline
          terenty
          wrote on last edited by
          #4

          Hi! You dont have to create multiple QPainter objects or multiple painter events, you need to draw multiple dots using one painter inside one painter event, smthng like this:

          @void Lines::drawLines(QPainter *qp)
          {
          timer_interval++;

          QPen pen(Qt::black, 2, Qt::SolidLine);
          qp->setPen(pen);
          for(int i = 0; i < 50; i++){
          qp->drawPoint(2+timer_interval,50+i);
          }
          }@

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            Store the points in a member vector and use that for drawing:

            @
            class Lines : public QWidget
            {
            Q_OBJECT

            QTimer *timer1;
            QVector<QPoint> m_points;  // <<-- new
            

            public:
            Lines(QWidget *parent = 0);
            int timer_interval;

            protected:
            void paintEvent(QPaintEvent *event);
            void drawLines(QPainter *qp);
            public slots:
            void call_repaint();

            };
            @

            in the cpp:

            @
            void Lines::call_repaint()
            {
            // <<-- new start
            timer_interval++;
            m_points.append(QPoint(2+timer_interval,50));
            // <<-- new end
            repaint();
            }

            void Lines::drawLines(QPainter *qp)
            {
            QPen pen(Qt::black, 2, Qt::SolidLine);
            qp->setPen(pen);

            // <<-- new start
            QPoint pt;
            foreach(pt, m_points)
            {
                qp->drawPoint(pt);
            }
            // <<-- new end
            

            }
            @

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MrNoway
              wrote on last edited by
              #6

              Wow thanks Gerolf,

              surprisingly, this is working as I want, though I dont really understand how you set it up.
              if you have little time, you may want to explain this in few words.

              @ QVector<QPoint> m_points; // << What did you do right here, never seen this <QPoint> before an variable @

              and this one here
              @ QPoint pt;
              foreach(pt, m_points)
              {
              qp->drawPoint(pt);
              }@

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MrNoway
                wrote on last edited by
                #7

                ok anyway, just marked it as solved.

                thanks again

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  Hi MrNoway,

                  You should have a look at the docs for these classes :-)

                  • QPoint is just what the name says, a point. x and y.
                  • A QVector<QPoint> is a dynamic array of QPoint objects
                  • foreach is a for each loop (shortcut for for(i = 0; i < vec.count(), ++i) {pt = vec[i]; ...)

                  These are helpers that Qt provides the users :-)

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  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