[Qt]使用QtConcurrent运行类成员函数
接我的上一篇博文《[Qt]一个简单的Qt Widget多线程处理图像的例子》。链接: https://powful1.blog.163.com/blog/static/5662321620152911372943/?newFollowBlog
同时我也只是在Dialog上面增加了一个按钮“QtConcurrent灰度化”。如下图:
然后在MainDialog中增加了一个按钮对应的槽函数和一个处理图像的函数(其实就是把MyImageProcessThread中的run函数复制了过来改了个名字叫做“ QImage* MainDialog::processImageByQtConcurrent() ”)。使用QtConcurrent的代码如下:void MainDialog::on_pushButton_toGrayImage_QConcurrent_clicked(){ QFuture<QImage*> future = QtConcurrent::run(this,&MainDialog::processImageByQtConcurrent); //imageAfterProceess 这个指针变量之前被无视,现在终于找到存在感了 this->imageAfterProceess = future.result(); //接收处理结果 this->showImageInNewWindow(this->imageAfterProceess);//新窗口显示处理后的图像}
运行结果如下,同样的代码处理一张8K的图像,QtConcurrent运行结果略快。
最后,附上Qt给出的官方文档,其实官方已经说得很好了。我也就现学现卖一下。https://qt-project.org/doc/qt-4.8/qtconcurrentrun.html
评论