[Qt4] QThread, QTimer -> wie ist es richtig?
Verfasst: 26. Februar 2006 02:33
hallo,
ich moechte in einem qthread eine funktion implementieren, die in regelmaessigen abstaenden (1 sekunde) im hauptprogramm ein qlabel mit der aktuellen uhrzeit aktualisiert. beide varianten funktionieren prinzipiell - nur welche wuerdet ihr vorziehen und warum?
variante 1:
variante 2:
73!
andy/dg0jaj
ich moechte in einem qthread eine funktion implementieren, die in regelmaessigen abstaenden (1 sekunde) im hauptprogramm ein qlabel mit der aktuellen uhrzeit aktualisiert. beide varianten funktionieren prinzipiell - nur welche wuerdet ihr vorziehen und warum?
variante 1:
Code: Alles auswählen
Clock::Clock () : QThread () {
start ();
}
void Clock::run () {
QTimer *timer = new QTimer ();
connect (timer, SIGNAL (timeout ()), this, SLOT (update ()));
timer -> start (1000);
exec ();
}
void Clock::update () {
QMutexLocker locker(&mutex);
QTime time = QTime::currentTime ();
QString time_string = time.toString ("hh:mm:ss");
emit updateClock (time_string);
}
Code: Alles auswählen
Clock::Clock () : QThread () {
start ();
}
void Clock::run () {
forever {
QMutexLocker locker(&mutex);
QTime time = QTime::currentTime ();
QString time_string = time.toString ("hh:mm:ss");
emit updateClock (time_string);
msleep (1000);
}
}
andy/dg0jaj