我的 QFileSystemModel
派生类中有函数 export()
。现在我想显示进度条,因为此功能有效。我显然不想从那里弹出 QProgressDialog
,因为 GUI 应该是独立的。
void MainWindow::on_pushButtonConvert_clicked()
{
QString rootPath = ui->lineEditSourceFolder->text();
QString destPath = ui->lineEditDestFolder->text();
dirModel->convert(rootPath, destPath); // dirModel is QFileSystemModel derived member variable
}
在将 convert()
移动到模型之前,它在我的 MainWindow
类中。此函数本身正在创建 QProgressDialog
,但现在在进入模式后,应该禁止创建它,那么我应该在哪里创建进度?
我从另一篇文章中得到提示,我应该使用信号和插槽,但如何使用?
最佳答案
您应该将 dirModel
移动到一个新线程,以防止 export()
阻塞主线程和 UI。这可以像这样完成:
QThread * th = new QThread();
dirModel->moveToThread(th);
QObject::connect(th,SIGNAL(started()),dirModel,SLOT(OnStarted()));
QObject::connect(th,SIGNAL(finished()),dirModel,SLOT(OnFinished()));
th->start();
dirModel
中的初始化和终止任务应分别在 OnStarted()
和 OnFinished()
槽中完成。
您应该在您的类中使用一个信号来通知用户界面中的进度条进度值。在您的 export()
函数中,您应该发出具有适当值的信号。信号是这样的:
void progressChanged(int val);
您还应该将 progressChanged(int)
信号连接到 QProgressBar
的 setValue(int value)
插槽。
最后一点是当 export()
在其他线程中时,您不应该直接调用它。正确的方法是将 export()
定义为插槽,并将信号连接到该插槽,并在您要调用 export()
时发出信号。
关于c++ - 如何显示模型中函数的进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28459518/