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. Newbie question: How to spin a BusyIndicator while loading file data?
QtWS25 Last Chance

Newbie question: How to spin a BusyIndicator while loading file data?

Scheduled Pinned Locked Moved Unsolved General and Desktop
busy indicatorworkerthread
3 Posts 3 Posters 1.1k 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.
  • T Offline
    T Offline
    Tom asso
    wrote on 17 Sept 2021, 00:06 last edited by Tom asso
    #1

    Just starting out with Qt threading api and have some questions. My C++/QML app reads data from a large file and displays it; the QML-defined GUI lets the user modify the display (e.g. with color maps, view angles, etc), and also provides a menu item to load data from another file. While loading data, I would like a QML BusyIndicator to spin, until the data is ready to display. While the BusyIndicator is spinning, the user should be prevented from trying to modify the display through the GUI. I realize the GUI thread must be running in order to spin the BusyIndicator, so presumably my GUI thread code should at least set the BusyIndicator.running property, and launch a worker thread which actually opens the new file and loads its data.

    • What’s the best way to run a worker thread with Qt’s api?
    • How do I prevent user interaction with other GUI controls while the worker thread is doing its job?

    Thanks!

    J 1 Reply Last reply 18 Sept 2021, 09:00
    0
    • A Offline
      A Offline
      AxelVienna
      wrote on 18 Sept 2021, 06:24 last edited by
      #2

      By BusyIndicator, you probably mean the Wait cursor.
      To set it:

      QApplication::setOverrideCursor(Qt::WaitCursor);
      

      To restore the normal cursor:

      QApplication::restoreOverrideCursor();
      

      Setting the wait cursor does not prevent UI interaction by the user.
      Such prevention depends on how your UI elements trigger programmatic actions. Most probably these actions are triggered by Qt's signal/slot mechanism. The blockSignals(const bool &) of an UI element (widget etc.) controls if signals are being fired or not. So if, during your data load operation, you want to prevent certain UI interactions, you want to do the following:

      // somewhere in your constructor
      connect(this,&yourClass::sig_loadingFinished, this,&yourClass::slot_afterLoadingFinished);
      
      void yourClass::startLoading(void)
      {
          QApplication::setOverrideCursor(Qt::WaitCursor); // set wait cursor
      
          // Block signals
          ui->yourWidget->blockSignals(true);
          ui->anotherWidget->blockSignals(true);
          ui->thirdWidget->blockSignals(true);
      
          // start your loading thread here
      }
      
      void yourClass::load(void)
      {
          // load your data
      
          // fire signal
          emit sig_loadingFinished();
      }
      
      void yourClass::slot_afterLoadingFinished()
      {
      // don't forget to unblock signals or your user will be hand cuffed
          ui->yourWidget->blockSignals(false);
          ui->anotherWidget->blockSignals(false);
          ui->thirdWidget->blockSignals(false);
      
          QApplication::restoreOverrideCursor(); // restore override cursor
      }
      

      The best way to run your worker thread really depends on what it is supposed to do, how you want to start it, how you want to allocate and free heap memory etc. You may want to post some code.

      C++ and Python walk into a bar. C++ reuses the first glass.

      1 Reply Last reply
      1
      • T Tom asso
        17 Sept 2021, 00:06

        Just starting out with Qt threading api and have some questions. My C++/QML app reads data from a large file and displays it; the QML-defined GUI lets the user modify the display (e.g. with color maps, view angles, etc), and also provides a menu item to load data from another file. While loading data, I would like a QML BusyIndicator to spin, until the data is ready to display. While the BusyIndicator is spinning, the user should be prevented from trying to modify the display through the GUI. I realize the GUI thread must be running in order to spin the BusyIndicator, so presumably my GUI thread code should at least set the BusyIndicator.running property, and launch a worker thread which actually opens the new file and loads its data.

        • What’s the best way to run a worker thread with Qt’s api?
        • How do I prevent user interaction with other GUI controls while the worker thread is doing its job?

        Thanks!

        J Offline
        J Offline
        jeremy_k
        wrote on 18 Sept 2021, 09:00 last edited by jeremy_k
        #3

        @Tom-asso said in Newbie question: How to spin a BusyIndicator while loading file data?:

        Just starting out with Qt threading api and have some questions. My C++/QML app reads data from a large file and displays it; the QML-defined GUI lets the user modify the display (e.g. with color maps, view angles, etc), and also provides a menu item to load data from another file. While loading data, I would like a QML BusyIndicator to spin, until the data is ready to display. While the BusyIndicator is spinning, the user should be prevented from trying to modify the display through the GUI. I realize the GUI thread must be running in order to spin the BusyIndicator, so presumably my GUI thread code should at least set the BusyIndicator.running property, and launch a worker thread which actually opens the new file and loads its data.

        • What’s the best way to run a worker thread with Qt’s api?

        Do you plan to allow cancellation of loading?

        • How do I prevent user interaction with other GUI controls while the worker thread is doing its job?

        Item has an enabled property that controls mouse and keyboard input for the item and its children. Eg:

        import QtQuick 2.12
        import QtQuick.Window 2.12
        
        Window {
            contentItem.enabled: false
            TextInput {
                anchors.centerIn: parent
                text: "This input is disabled by its parent"
            }
        }
        

        @AxelVienna said in Newbie question: How to spin a BusyIndicator while loading file data?

        QApplication::setOverrideCursor(Qt::WaitCursor);

        QGuiApplication, not QApplication. There's no need to involve widgets for a QML/Quick application.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        2

        2/3

        18 Sept 2021, 06:24

        • Login

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