Repaint() function paints only last point!!



  • @void Widget::paintEvent(QPaintEvent *eve){
    QPainter p(this);
    QPen pen(Qt::white,1,Qt::SolidLine);
    p.setPen(pen);
    if(f==1){
    qDebug()<<x;
    p.drawPoint(x,y);
    f=0;
    }

    }

    void Widget::func()
    {
    for(int i=0;i<100;i++){
    f=1;
    x++;
    repaint();
    qDebug()<<x;

    }
    

    }
    @



  • Hello and welcome to the forum!

    AFAIK you should not call paintEvent so often. The loop on your points you have in your Widget::func method should rather be in the Widget::paintEvent method. But I have no clue if this will solve your problem.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    As Johan said, calling repaint like that is a bad idea. If you want to animate the painting you should rather use e.g. a QTimer.

    Also, you have to pay attention on the value of x, just incrementing it will make it larger than your widget.



  • The actual func() is line drawing algorithms,dda()
    And bresenham(). I had written these functions in paintevent itself but the teacher told me to make separate functions.
    Hence i had to call repaint().
    The algorithm increments or decrements the global variables x and y according to the algorithm and i expect repaint to draw the points.
    Hope i made my point clear.
    Thank you



  • Hi, repaint() is usually called when you want to clean or refresh your widget's screen area, what I mean: you call "erase screen" 100 times and drawPoint() 100 times :-)

    Best is to do what SGaist suggests,use a QTimer.

    Or you can change your code in Widget::paintEvent() so that first time it's called it draws just one point; then next time it's called, redraw previous point and one more, next time draw 3 points etc.


  • Lifetime Qt Champion

    Breaking a big function in smaller pieces doesn't mean you need to redo your logic. If your teacher was happy with your first implementation, just keep it and isolate each part


Log in to reply
 

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