QT进度条显示功能的状态 [英] QT Progress bar showing status of a function
问题描述
最近我正在使用QT的点云图书馆项目。
例如,我创建了一个类名系统,其公共函数名为 estimate_normal
。在这个 estimate_normal
内,我从PCL库调用一些函数,在MainWindow.cpp中调用
system a;
a.estimate_normal();
我可以通过使用QT进度条看到这个 estimate_normal
status?
感谢
a = new system();
QThread * th = new QThread();
a-> moveToThread(th);
QObject :: connect(th,SIGNAL(started()),a,SLOT(OnStarted()));
QObject :: connect(th,SIGNAL(finished()),a,SLOT(OnFinished()));
th-> start();
类system中的初始化和终止任务应该在OnStarted()和OnFinished )槽。
您应该使用类system中的信号来通知用户界面中进度条的进度条。在你的estimate_normal()函数中,你应该发出具有适当值的信号。信号如下:
void progressChanged(int val);
您还应该将progressChanged(int)信号连接到QProgressBar的setValue 。
最后一点是,在其他线程中,不应直接调用estimate_normal()。正确的方法是将estimate_normal()定义为一个插槽,并将信号连接到该插槽,并在您想调用estimate_normal()时发出信号。
recently I'm working on Point cloud Library project with QT. I'd like to know if I can know the current progress when I use a function.
For example, I create a class name system with a public function named estimate_normal
. Inside this estimate_normal
, I call some functions from the PCL library, and in MainWindow.cpp I call
system a;
a.estimate_normal();
Can I know the progress by using QT Progress bar to see this estimate_normal
status?
Thanks
You should create your object of the class "system" on the heap and move it to a new thread in order to prevent estimate_normal() from blocking main thread and the UI. This can be done like:
a = new system();
QThread * th = new QThread();
a->moveToThread(th);
QObject::connect(th,SIGNAL(started()),a,SLOT(OnStarted()));
QObject::connect(th,SIGNAL(finished()),a,SLOT(OnFinished()));
th->start();
Your initialization and termination tasks in the class "system" should be done in OnStarted() and OnFinished() slots respectively.
You should use a signal in the class "system" to notify the progress bar in your user interface of the value for the progress. In your estimate_normal() function you should emit the signal with the appropriate value. The signal is like:
void progressChanged(int val);
You should also connect the progressChanged(int) signal to the setValue(int value) slot of the QProgressBar.
And the last point is that you should not call estimate_normal() directly when it is in an other thread. The correct way is defining estimate_normal() as a slot and connecting a signal to that slot and emitting the signal when you want to call estimate_normal().
这篇关于QT进度条显示功能的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!