Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Need to understand setUpdatesEnabled behaviour while updating UI elements
Forum Updated to NodeBB v4.3 + New Features

Need to understand setUpdatesEnabled behaviour while updating UI elements

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 648 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    starkm42
    wrote on last edited by
    #1

    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.

    1. i need to understand how QT is performing updates to UI, any article explaining this particular scenario or answer is much appreciated.
    2. how can i do tasks like these, periodic updates to UI (often bulk) efficiently.

    ps:
    i am new to QT.

    jsulmJ 1 Reply Last reply
    0
    • S starkm42

      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.

      1. i need to understand how QT is performing updates to UI, any article explaining this particular scenario or answer is much appreciated.
      2. how can i do tasks like these, periodic updates to UI (often bulk) efficiently.

      ps:
      i am new to QT.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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().

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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().

        S Offline
        S Offline
        starkm42
        wrote on last edited by
        #3

        @jsulm

        yup, that fixed it, thanks !

        do you have any advice on my second question regarding bulk updates, do i do them the same as i am doing it here ?

        jsulmJ 1 Reply Last reply
        0
        • S starkm42

          @jsulm

          yup, that fixed it, thanks !

          do you have any advice on my second question regarding bulk updates, do i do them the same as i am doing it here ?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @starkm42 If you want to do something periodically then use QTimer.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • S starkm42 has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved