Setting Timer Event for Drawing Ellipse

  • Hello,

    I have to set a timer event in order to draw an circle in the middle, then erase (or delete) the current circle and then draw a new circle as bigger diameter (from inner to outer). I couldnt define the timer event and delete the current circle. Could someone give advice how to organize(write) it correctly. Thank you in advance. The code "grafikspielerei.cpp" ,header file "grafikspielerei.h" and "main.cpp" are included.

    Thank you in advance

    #include "grafikspielerei.h"
    #include <QTimer>
    #include <ctime>
    #include <QMessageBox>
        resize(400, 400);
        timercircle = new QTimer(this);
        //connect the timer with the Slot
        QObject::connect(timercircle, SIGNAL(timeout()), this, SLOT(timercircleSlot()));
    void Grafikspielerei::paintEvent(QPaintEvent *)
    void Grafikspielerei::kreise()
        int x=195;
        int y=195;
        int breite, hoehe;
        //create an instance
        QPainter myPainter(this);
        //zeiger = new QPainter();
        breite = width()-390;
        hoehe = height()-390;
        for (int j = 0; j < 10; j++)
            //QMessageBox::information(this, "Beginning", "Timer startet");
            for (int i=0; i < 1; i++)
            //drawing the circle
            myPainter.drawEllipse(x, y, breite, hoehe);
        //changing the position of circle center
        x = x - 20;
        y = y - 20;
        //width / height of the circle changing from the middle to the outer place
        breite = breite + 40;
        hoehe = hoehe + 40;
    void Grafikspielerei::timercircleSlot()
    #include <QtGui>
    #include <QWidget>
    #include <QTimer>
    #include <QPushButton>
    class Grafikspielerei : public QWidget
        QTimer *timercircle;
        QPainter *zeiger;
    //die ├╝berschriebene Methode
        void paintEvent(QPaintEvent *);
    //die Methoden zum Zeichnen
        void rechteck();
        void linie();
        void linie2();
        void kreis();
        void polygon();
    private slots:
        void kreise(); //draw circles
        void timercircleSlot(); //timer for circle

    #include "grafikspielerei.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    Grafikspielerei w;;

    return a.exec();


  • @cprogcoder sorry if it obvious, but from your code I cannot see where you set the time interval for your timer and where you start it. Have you check QTimer documentation?

  • Lifetime Qt Champion


    Your timer is likely stoped before it can timeout as all the painting will happen faster than one second but even if not, the logic if flowed. You are basically restarting it ten times, it's not a "loop stopper".

Log in to reply

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