this is the run method of a subclass of QThread:
void crono::Controller::run() {
//initialise timer
int *i = & this->model->seconds_elapsed;
for (*i = 0; *i < this->model->seconds_total; (*i)++) {
//calculate current seconds/minutes/hours elapsed starting from seconds_elapsed (*i)
this->model->tick();
//should display in the form the time elapsed
this->vista->showTime();
sleep(1000);
}
Beep(1000, 500); //beep when all is over
}
the controller updates the model values.
The QT form is opened on start, I guess in the main application thread.
The issue is that despise on debug *i=0 and seconds_total = X > 0, the loop is executed only one time, after the first time the debug halt (it does not end), the forms pops up but nothing happens.
The only thing I can guess of, is that the Controller Thread lose its priority and never gains the cpu again.
How can avoid this?
EDIT I am trying using QTimer, with bad luck.
i declared update as a public slot, and then implemented like this:
void crono::Controller::update() {
this->modello->tick();
this->vista->showTime();
//eventually stop at some point (pointer to timer and timer->stop()?
//...
//Beep(1000, 500);
}
And I inserted the QTimer in the controller (thread) object instead of the loop cycle:
void crono::Controller::run() {
//inizializzo timer
int *i = & this->modello->secondi_trascorsi;
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()),this, SLOT(update()));
timer->start(1000);
}
I do not know why, but the update() method is never called, instead of being called an infinite number of times. Why?
QThread::run(), you need to callexec()at the end of therun()method, otherwise any events (including signals) will never be processed by the thread.