Unsolved Low performance, timer and label.
-
@Loc888 said in Low performance, timer and label.:
Give me back my wasted time
I could say the same here.
If you want help, a bit of humility and accepting the fact that there's a 99.9% chance the problem is in your code and not in Qt is a first step
-
@VRonin Ther is nothing to post, simple Qtimer and settext function. That's it.
The timer refire the function, and set the current time. -
Can you try this basic example?
#include <QApplication> #include <QTimer> #include <QLabel> int main(int argc, char *argv[]) { QApplication a(argc, argv); QLabel mainWid; QTimer mainTimer; int counter = 0; QObject::connect(&mainTimer,&QTimer::timeout,[&mainWid,&counter]()->void{ mainWid.setText(QObject::tr("%1 sec").arg(++counter)); }); mainTimer.start(1000); mainWid.show(); return a.exec(); }
-
@VRonin It says no matching function.
-
@Loc888 I tested it on my machine Qt 5.10 MSVC2013 x64 Win7. What version of Qt are you running?
-
@VRonin Desktop Qt 5.2.0 MinGW 32bit
-
Then everything should work, you probably just have to enable C++11
- add
CONFIG += c++11
in your.pro
file - re-run qmake
- add
-
@VRonin It's working, but i need it in MS. It happens because text need to be updated fast, and it cause the problem. If i just run the timer, it's look's fine, but i need to show the time in label..
-
@Loc888 I think you need to be a bit more specific, with what is going on.
It's working, but i need it in MS. It happens because text need to be updated fast, and it cause the problem. If i just run the timer, it's look's fine, but i need to show the time in label..
Updating a QLabel's text shouldn't take seconds to update
to modify the code of @VRonin abit to display time and fast changes:
#include <QApplication> #include <QTimer> #include <QLabel> #include <QTime> int main(int argc, char *argv[]) { QApplication a(argc, argv); QLabel mainWid; QTimer mainTimer; QObject::connect(&mainTimer,&QTimer::timeout,[&mainWid]()->void{ mainWid.setText(QTime::currentTime().toString("hh.mm.ss.zz")); }); mainTimer.start(10); mainWid.show(); return a.exec(); }
-
@J.Hilk I would like to use that, but i don't want current time, i want everything starting from 0.
-
@Loc888
this code examples are for the purpose of trouble shooting.
But it's easy enought to adapt.#include <QApplication> #include <QTimer> #include <QLabel> #include <QTime> #include <QElapsedTimer> int main(int argc, char *argv[]) { QApplication a(argc, argv); QLabel mainWid; QTimer mainTimer; QTime time(0,0); QElapsedTimer actualTimePassed; QObject::connect(&mainTimer,&QTimer::timeout,[&mainWid, &time, &actualTimePassed]()->void{ time = time.addMSecs(actualTimePassed.elapsed()); actualTimePassed.start(); mainWid.setText(time.toString("hh.mm.ss.zzz")); }); actualTimePassed.start(); mainTimer.start(10); mainWid.show(); return a.exec(); }
-
@Loc888 said in Low performance, timer and label.:
it's working, but i need it in MS
Unfortunately this is platform dependant. Best you could do is:
#include <QApplication> #include <QTimer> #include <QLabel> int main(int argc, char *argv[]) { QApplication a(argc, argv); QLabel mainWid; QTimer mainTimer; mainTimer.setTimerType(Qt::PreciseTimer); unsigned int counter = 0; QObject::connect(&mainTimer,&QTimer::timeout,[&mainWid,&counter]()->void{ mainWid.setText(QObject::tr("%1 msec").arg(++counter)); mainWid.repaint(); }); mainTimer.start(1); mainWid.show(); return a.exec(); }
Please note, from http://doc.qt.io/qt-5/qt.html#TimerType-enum:
Precise timers try to keep millisecond accuracy
try != will, unfortunately
-
@VRonin Maybe i expressed wrong, but i don't want it in Ms (the total), so 5500 = 5,5s, i want that pattern "mm.ss.zzz" or "mm.ss.zz" , but i want it to start from 0, not from the current time. Is any way to force it? (In sec, ther is no delay when the method update the text).
-
@J.Hilk Guys,i tried J.Hilk code,and it's not working how it should.
It doesnt help me, it's working, but if i compare the timer to my windows time, the difference is arround 10s (after 2min test), it's too mutch.
Why it's happening?
Ps. I tried with "setTimerType(Qt::PreciseTimer);", but still doesn't help...
????????
-
My turn ;)
QLabel label; label.setGeometry(100,100,200,50); QTimer timer; QTime time; QObject::connect(&timer,&QTimer::timeout,[&time,&label](){ QTime elapsed=QTime::fromMSecsSinceStartOfDay(time.elapsed()); label.setText(elapsed.toString("mm:ss:zzz")); }); time.start(); timer.start(100); label.show();
-
It seems you are the winner... Why it's working?
I thought it's working, because you set the delay to 100ms, but even if i change it to 1ms, still working 100%.
I don't understand why this is working,and the other timers including my, are all retarded... Why it's happening?And that's the text update method fault's, i tried to turn off the update, and just set the last result, and it's 100% accurate.
-
I know by experience that to rely on a timer is all wrong.
You must rely on the system clock for good accuracy.And that's the text update method fault's, i tried to turn off the update, and just set the last result, and it's 100% accurate.
Refresh 1000 times per sec is a heavy task, it consumes about 20% cpu on my machine and serves nothing.
Timers use the event loop and if the computer is doing heavy tasks, timers triggers might be lost.
Rely on event loops for a counter is definitely a bad idea. -
@mpergand Ok, how i can use it with Start, Stop, and Reset button? Because i am trying to move your code to another project, and work with buttons, but when i start the timer, it doesn't do anything.. Still be on 00:00:000, i run it ones, and it start from the actual minutes and seconds, then i modified your code to force it start from 0, and i broke something, now nothing is working...
-
@mpergand Ok, nevermind, i am blind sometimes... I fix it.
I find the way to restart it, but i have some problems with
" time.fromString("05:00:000"); "
It's not working, how should i use that? Or how to stop the time and the timer, and re-start it again but from the last time? (I dont want to reset it when i press the stop button, and then start).
-
You missed an argument and the fact that it's a static function: http://doc.qt.io/qt-5/qtime.html#fromString-1
time = QTime::fromString("05:00:000","mm:ss:zzz");
Have you considered
QTimeEdit
instead of QLabel?