最近我正在使用 QT 进行点云库项目。我想知道当我使用某个函数时是否可以知道当前的进度。
例如,我创建了一个类名系统,其中包含一个名为 estimate_normal
的公共(public)函数。在此 estimate_normal
中,我调用 PCL 库中的一些函数,并在 MainWindow.cpp 中调用
system a;
a.estimate_normal();
我可以通过使用 QT 进度条查看此 estimate_normal
状态来了解进度吗?
谢谢
最佳答案
您应该在堆上创建“system”类的对象并将其移动到新线程,以防止estimate_normal()阻塞主线程和UI。这可以这样做:
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(int value) 插槽。
最后一点是,当estimate_normal()在其他线程中时,你不应该直接调用estimate_normal()。正确的方法是将estimate_normal()定义为一个槽,并将信号连接到该槽,并在您想要调用estimate_normal()时发出信号。
关于c++ - 显示功能状态的 QT 进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22551978/