Delay in the output



  • Hello,

    im start today with the concept of QT.

    My first aim is to create a grid. This work fine.

    My Code is:

    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    
    QBrush greenBrush(Qt::green);
    QBrush blueBrush(Qt::blue);
    QPen outlinePen(Qt::black);
    //outlinePen.setWidth(1);
    
    
    
    //rectangle = scene->addRect( 0, 0, 5, 5, outlinePen, blueBrush);
    
    
    for(int i = 0;i <= 500; i = i +10){
    
        for(int j = 0;j <= 500; j = j +10)
        {
            rectangle = scene->addRect( QRectF(i, j, 1, 1), outlinePen, blueBrush);
        }
    
    }
    

    }
    @

    My problem is to get every rectangle with a delay. For example 50 ms.
    With the system function sleep, the application stucks.

    How i could solve the problem?

    Thx Andre



  • Hi,
    sleep shouldn't be used at all within Qt. Even not with multithreading.
    What you need is a timer of 50msec which will paint a new rectangle every time the timer expires. Check out the QTimer classes and connect a slot to the expired signal.



  • Hello,

    thx for response.

    i have integrate a timer class into the project, but i cant connect the Timer with the draw function:

    This is the code.

    mytimer.h:

    @
    #ifndef MYTIMER_H
    #define MYTIMER_H

    #include <QTimer>

    class MyTimer : public QObject
    {
    Q_OBJECT
    public:
    MyTimer();
    QTimer *timer;

    public slots:
    void MyTimerSlot();
    };

    #endif // MYTIMER_H
    @

    mytimer.cpp
    @
    #include "mytimer.h"
    #include <QDebug>

    MyTimer::MyTimer()
    {
    // create a timer
    timer = new QTimer(this);

    // setup signal and slot
    connect(timer, SIGNAL(timeout()),
          this, SLOT(MyTimerSlot()));
    
    // msec
    timer->start(1000);
    

    }

    void MyTimer::MyTimerSlot()
    {
    qDebug() << "Timer...";
    }
    @

    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsItem>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;

    QGraphicsScene *scene;
    
    QGraphicsRectItem *rectangle;
    

    };

    #endif // MAINWINDOW_H
    @

    mainwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    
    
    
    QBrush greenBrush(Qt::green);
    QBrush blueBrush(Qt::blue);
    QPen outlinePen(Qt::black);
    //outlinePen.setWidth(1);
    
    //rectangle = scene->addRect( 0, 0, 5, 5, outlinePen, blueBrush);
    
    
    
    
    for(int i = 0;i <= 500; i = i +10){
    
        for(int j = 0;j <= 500; j = j +10)
        {
            rectangle = scene->addRect( QRectF(i, j, 1, 1), outlinePen, blueBrush);
        }
    
    }
    

    /*
    for(int j = 0;j <= 500; j = j +10)
    {
    rectangle = scene->addRect( QRectF(j, j, 1, 1), outlinePen, blueBrush);
    }
    */
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    main.cpp
    @
    #include "mainwindow.h"
    #include <QApplication>
    #include "MyTimer.h"

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

    MainWindow w;
    
    w.show();
    
    MyTimer timer;
    
    return a.exec&#40;&#41;;
    

    }
    @

    Does anyone have a suggestion?

    Thx
    Andre



  • Hi,
    i don't see any draw function in your code. Create separate draw function as a slot in MainWindow, and connect this slot with timeout() QTimer signal in main window construcrot. Also, you can see the Animation Framework in Qt examples. Good luck!



  • Hi,

    thx for help.

    the animation for a diagonal line is running, this is the same like a single for loop.

    Does I need a second timer, If I want to design a two dimensional "for loop" for drawing a grid?

    The modification of the code ist:

    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QGraphicsItem>

    #include <QTimer>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    QTimer *timer2;
    

    public slots:
    void draw();

    public:
    Ui::MainWindow *ui;

    QGraphicsScene *scene;
    
    QGraphicsRectItem *rectangle;
    
    int i;
    

    };

    #endif // MAINWINDOW_H

    @

    mainwindow.cpp

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include <QDebug>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    
    
    timer2 = new QTimer(this);
    
    // create a timer
    
    
    i = 0;
    
    
    connect(timer2, SIGNAL(timeout()), this, SLOT(draw()));
    // msec
    timer2->start(1000);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::draw()
    {
    QBrush greenBrush(Qt::green);
    QBrush blueBrush(Qt::blue);
    QPen outlinePen(Qt::black);

     /*
     for(int i = 0;i <= 500; i = i +10){
    
         for(int j = 0;j <= 500; j = j +10)
         {
             rectangle = scene->addRect( QRectF(i, j, 1, 1), outlinePen, blueBrush);
         }
    
     }
     */
    
     rectangle = scene->addRect( QRectF(i, i, 1, 1), outlinePen, blueBrush);
     i +=10;
    
     qDebug() << "Draw...";
    

    }

    @

    main.cpp
    @
    #include "mainwindow.h"
    #include <QApplication>

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

    MainWindow w;
    
    w.show();
    
    return a.exec&#40;&#41;;
    

    }

    @



  • Hi,

    i think one solution, to paint a grid is:

    @

    void MainWindow::draw()
    {
    QBrush greenBrush(Qt::green);
    QBrush blueBrush(Qt::blue);
    QPen outlinePen(Qt::black);

     /*
     for(int i = 0;i <= 500; i = i +10){
    
         for(int j = 0;j <= 500; j = j +10)
         {
             rectangle = scene->addRect( QRectF(i, j, 1, 1), outlinePen, blueBrush);
         }
    
     }
     */
    
    
    
     rectangle = scene->addRect( QRectF(i, j, 1, 1), outlinePen, blueBrush);
    
    
    
     i +=10;
    
    
     if (i % 500 == 0 )
     {
         j += 10;
         i = 0;
         z++;
     }
    
     qDebug() << "Draw...\n";
     qDebug() << i ;
     qDebug() << "\n";
     qDebug() << j ;
     qDebug() << "\n";
    
     if (z  == 50)
        timer2->stop();
    

    }

    @

    but i dont think it is an elegant way.

    Does someone has a better solution ?

    Thx Andre


Log in to reply
 

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