Qt - How to load data/process data after QWidget has been shown ?
-
HI ,
When i am trying to load heavy data into a QTreeView, it is taking time to come up. Is there a way to do it after showing the QWidget?how to load widgets smoothly so that they don't have to wait for laoding all the data into them.
Any help is appreciated
Thanks
-
You can try the Model-View way. As i know model fetches data only when needed.
-
Well, if you are loading data into a QTreeView you have to be already using the Model-View design pattern.
A way (maybe not the best) to get what I guess you want, is creating a function in your project class where you could pass the Model pointer to QTreeView, to then call this function in the main.cpp (after *.show()).
Something like this: lets say your project class is named "Test" and the function "showData", so:
test.h:
@
class Test : public QMainWindow
{Q_OBJECT
public:
explicit Test(QWidget *parent = 0); ~Test(); ... void showData(); ...
};
@test.cpp:
@
...void Test::showData() {
ui->treeView->setModel(&model);
}
...
@... and then in your main.cpp, call showData() after w.show():
main.cpp:
@
int main(int argc, char *argv[]) {QApplication a(argc, argv);
Test w;
w.show();
w.showData();
return a.exec();
}
@Another suggestion is to review the way about how you have implemented the load of your data, focused on performance.
...in this case this articles could help:
"Performance Tip Optimizing Containers":http://developer.qt.nokia.com/wiki/Performance_Tip_Optimizing_Containers
"Performance Tip Optimizing Iteration":http://developer.qt.nokia.com/wiki/Performance_Tip_Optimizing_Iteration
"Iterating Efficiently":http://labs.qt.nokia.com/2009/01/23/iterating-efficiently/
...I hope this has been some way useful for you.
-
Isn't all of that still freezing his application since the event loop processes clicking and painting events?
-
Another option is to separate the model's code that gathers the data into a background process and to notify the model of the data dropping in. This it the way the [[Doc:QFileSystemModel]] works. Make sure to read the excellent article on "Threads, Events and QObjects":/wiki/Threads_Events_QObjects to avoid common problems and pitfalls.