Ok. I known problem. You should use qApp->processEvents(); in while loop. I write simple code with two type of timer. Try it.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QBasicTimer>
#include <QTimer>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::MainWindow *ui;
int isRun;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QBasicTimer>
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void timerEvent(QTimerEvent * event);
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::MainWindow *ui;
int isRun;
QBasicTimer *basictimer;
QTimer *timer;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
isRun = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
timer->setSingleShot(true);
timer->start(5000);
int i=0;
while(timer->isActive())
{
++i;
ui
->label
->setText
("Running " + QString::number(i
));
qApp->processEvents();
}
ui->label->setText("Stopped");
delete timer;
}
void MainWindow::on_pushButton_2_clicked()
{
isRun=1;
basictimer->start(5000, this);
int i=0;
while(isRun)
{
++i;
ui
->label
->setText
("Running " + QString::number(i
));
qApp->processEvents();
}
ui->label->setText("Stopped");
delete basictimer;
}
{
if(event->timerId()==basictimer->timerId())
{
basictimer->stop();
isRun=0;
}
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
isRun = 0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
timer = new QTimer();
timer->setSingleShot(true);
timer->start(5000);
int i=0;
while(timer->isActive())
{
++i;
ui->label->setText("Running " + QString::number(i));
qApp->processEvents();
}
ui->label->setText("Stopped");
delete timer;
}
void MainWindow::on_pushButton_2_clicked()
{
basictimer = new QBasicTimer();
isRun=1;
basictimer->start(5000, this);
int i=0;
while(isRun)
{
++i;
ui->label->setText("Running " + QString::number(i));
qApp->processEvents();
}
ui->label->setText("Stopped");
delete basictimer;
}
void MainWindow::timerEvent(QTimerEvent * event)
{
if(event->timerId()==basictimer->timerId())
{
basictimer->stop();
isRun=0;
}
else QMainWindow::timerEvent(event);
}
To copy to clipboard, switch view to plain text mode
Bookmarks