jack249
14th January 2013, 02:20
I've encounter a problem for having 2 timers.
I will use simple code below to explain on it.
static int a = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&timer_1,SIGNAL(timeout()),this,SLOT(on_timer_1_tim eout()));
connect(&timer_2,SIGNAL(timeout()),this,SLOT(on_timer_2_tim eout()));
timer_1.start(100);
timer_2.start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_timer_1_timeout()
{
a++;
bool b_out=true;
while(b_out)
{
QCoreApplication::processEvents(); //a breakpoint here
Sleep(500);
}
}
void MainWindow::on_timer_2_timeout()
{
a++;
bool b_out=true;
while(b_out)
{
QCoreApplication::processEvents(); //a breakpoint here
Sleep(500);
}
}
Once i start the program, the program is stop at the breakpoint inside on_timer_1_timeout(), the variable a is 1.
And then i continue the program, the program will stop at the breakpoint inside on_timer_2_timeout(), the variable a is 2.
After that, i keep on continue the program, the program will keep on stuck at on_timer_2_timeout() and the variable a is 2.
What should I do so that the program will back to on_timer_1_timeout() and remain the variable a is 2?
I've try on stating the connection is Qt::QueuedConnection, it is able to go to on_timer_1_timeout() but the variable a will keep on increasing and this is not what I want.
I will use simple code below to explain on it.
static int a = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&timer_1,SIGNAL(timeout()),this,SLOT(on_timer_1_tim eout()));
connect(&timer_2,SIGNAL(timeout()),this,SLOT(on_timer_2_tim eout()));
timer_1.start(100);
timer_2.start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_timer_1_timeout()
{
a++;
bool b_out=true;
while(b_out)
{
QCoreApplication::processEvents(); //a breakpoint here
Sleep(500);
}
}
void MainWindow::on_timer_2_timeout()
{
a++;
bool b_out=true;
while(b_out)
{
QCoreApplication::processEvents(); //a breakpoint here
Sleep(500);
}
}
Once i start the program, the program is stop at the breakpoint inside on_timer_1_timeout(), the variable a is 1.
And then i continue the program, the program will stop at the breakpoint inside on_timer_2_timeout(), the variable a is 2.
After that, i keep on continue the program, the program will keep on stuck at on_timer_2_timeout() and the variable a is 2.
What should I do so that the program will back to on_timer_1_timeout() and remain the variable a is 2?
I've try on stating the connection is Qt::QueuedConnection, it is able to go to on_timer_1_timeout() but the variable a will keep on increasing and this is not what I want.