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



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



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



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



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



  • 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
    

    }
    @



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



  • ok anyway, just marked it as solved.

    thanks again



  • 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 :-)


Log in to reply
 

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