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?
- How do I prevent user interaction with other GUI controls while the worker thread is doing its job?
Thanks!
-
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.
-
@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.