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. Repaint() function paints only last point!!
Forum Updated to NodeBB v4.3 + New Features

Repaint() function paints only last point!!

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 1.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.
  • O Offline
    O Offline
    onkar
    wrote on last edited by
    #1

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

    }
    

    }
    @

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

      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.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • O Offline
          O Offline
          onkar
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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