【问题标题】:How to show progress bar from a function in model?如何显示模型中函数的进度条?
【发布时间】:2015-05-25 18:42:05
【问题描述】:

我的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,但是现在进入模式后,应该禁止创建它,那么我在哪里创建进度呢?

我从另一篇帖子中得到提示,我应该使用信号和插槽,但这里怎么做?

【问题讨论】:

    标签: c++ qt progress-bar


    【解决方案1】:

    您应该将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) 信号连接到QProgressBarsetValue(int value) 插槽。

    最后一点是,当export() 在另一个线程中时,您不应该直接调用它。正确的方法是将export() 定义为一个插槽,并将信号连接到该插槽并在您想调用export() 时发出信号。

    【讨论】:

    • 谢谢,我认为这可以解决我遇到的其他问题,比如前几天我的互联网连接中断,应用程序长时间无法启动,因为它无法找到映射的驱动器.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    相关资源
    最近更新 更多