Need to understand setUpdatesEnabled behaviour while updating UI elements
-
Hi,
i have simple program that reads GPS values from NMEA log file and displays on MainWindow(subclass of QWidget).
here is how it is setup.
we have a class GPSSource that inherits QGeoPositionInfoSource and defines all pure virtual functions.
it provides a signal newGPSPositionUpdate every time a new GPS value is read from Log file
signals: void newGPSPositionUpdate(void);
main widget gets its update from connecting its update slot using this signal
connect(source, &GPSSource::newGPSPositionUpdate, this, QOverload<>::of(&MainWindow::update));
within paintEvent method of main Widget we have.
void MainWindow::paintEvent(QPaintEvent* /* *eventObj */) { // access last know position QGeoPositionInfo lastPosition = source->lastKnownPosition(); displayForm->latitude->setText(QString::asprintf("%fd", lastPosition.coordinate().latitude())); displayForm->longitude->setText(QString::asprintf("%fd", lastPosition.coordinate().longitude())); displayForm->timestamp->setText(lastPosition.timestamp().toString()); qDebug() << lastPosition.timestamp().toString(); }
GPSSource logs timestamp as it reads from log file and main Widget logs timestamp as it receives the signal and i was expecting Application output to be
"Thu Jan 1 12:36:05 1970" "12h 36m 5.57s November 2 2023" "Thu Nov 2 12:36:05 2023" "12h 36m 6.57s November 2 2023" "Thu Nov 2 12:36:06 2023" "12h 36m 6.57s November 2 2023"
what i am getting
"12h 36m 5.57s January 1 1970" "Thu Jan 1 12:36:05 1970" "Thu Jan 1 12:36:05 1970" "12h 36m 5.57s January 1 1970" "Thu Jan 1 12:36:05 1970" "12h 36m 5.57s November 2 2023" "Thu Nov 2 12:36:05 2023" "Thu Nov 2 12:36:05 2023" "12h 36m 6.57s November 2 2023"
using setUpdatesEnabled to toggle updates to bulk update UI elements
setUpdatesEnabled(false); displayForm->latitude->setText(QString::asprintf("%fd", lastPosition.coordinate().latitude())); displayForm->longitude->setText(QString::asprintf("%fd", lastPosition.coordinate().longitude())); displayForm->timestamp->setText(lastPosition.timestamp().toString()); setUpdatesEnabled(true);
I get
"12h 37m 8.57s November 2 2023" "Thu Nov 2 12:37:08 2023" "Thu Nov 2 12:37:08 2023" "Thu Nov 2 12:37:08 2023" "Thu Nov 2 12:37:08 2023" "Thu Nov 2 12:37:08 2023" "Thu Nov 2 12:37:08 2023"
my questions.
- i need to understand how QT is performing updates to UI, any article explaining this particular scenario or answer is much appreciated.
- how can i do tasks like these, periodic updates to UI (often bulk) efficiently.
ps:
i am new to QT. -
@starkm42 said in Need to understand setUpdatesEnabled behaviour while updating UI elements:
QGeoPositionInfo lastPosition = source->lastKnownPosition();
displayForm->latitude->setText(QString::asprintf("%fd", lastPosition.coordinate().latitude())); displayForm->longitude->setText(QString::asprintf("%fd", lastPosition.coordinate().longitude())); displayForm->timestamp->setText(lastPosition.timestamp().toString());
qDebug() << lastPosition.timestamp().toString();
Why are you doing this in paintEvent? You're not painting here, right? You just set some labels. Just connect a slot to newGPSPositionUpdate and set the labels in that slot. No need for update() and paintEvent().
-