Qt之QAxObject用法

来源:互联网 发布:2015年网络大电影票房 编辑:程序博客网 时间:2024/05/22 06:14

    QtConcurrent 是一个构建在QThreadPool之上的上层API,它用于处理最普通的并行计算模式:map, reduce, and filter。同时,QtConcurrent::run()方法提供了一种便于在另一个线程运行一个函数的方法。

    不像QThread 以及QRunnable,QtConcurrent 没有要求我们使用底层的同步原语,QtConcurrent 所有的方法会返回一个QFuture 对象,它包含了结果而且可以用来查询线程计算的状态(它的进度),从而暂停、继续、取消计算。QFutureWatcher 可以用来监听一个QFuture 进度,并且通过信号和槽与之交互(注意QFuture是一个基于数值的类,它并没有继承自QObject).

比如以下示例:

class MyWnd : public QWidget{Q_OBJECTprivate:static void execMyThr(void *pContext,string fileName,QString saveName);void execMy(string fileName,QString saveName);public slots: void onClickedMy();void MyFinished(QString filePath);private:bool bFinished;signals:void finished(QString);};
<span style="color:#363534;">#include <fstream>#include <QFileInfo>MyWnd::MyWnd(QWidget *parent): QWidget(parent),bFinished(true){connect(this,SIGNAL(finished(QString)),this,SLOT(MyFinished(QString)));}void MyWnd::onClickedMy(){if(!bFinished){QMessageBox::warning(this,tr("提示"),tr("上一次执行还未完成, 请稍候..."));return;}string filehs;QFileInfo file(QString::fromStdString(filehs));if(file.exists()){       ///......QtConcurrent::run(execExportThr,this,filehs,fileName);}}void MyWnd::execMy(string filehs,QString saveName){   </span><span style="color:#ff0000;">///不能在这个函数中调用QtGUI界面</span><span style="color:#363534;">bFinished = false;ifstream fread;fread.open(filehs.c_str(), ios::in | ios::binary);if(fread.good()){ExcelEngine excel;if(excel.IsInstalledExcel()){while(fread.read((char*)&log, sizeof(LogInfo))){}excel.Close();}fread.close();}emit finished(saveName);}void MyWnd::execMyThr( void *pContext,string fileName,QString saveName){MyWnd *pThis = (MyWnd*)pContext;pThis->execMy(fileName,saveName);}void MyWnd::MyFinished(QString saveName){QMessageBox::about(this,"成功",QString("执行完毕!"));bFinished = true;}</span>



0 0
原创粉丝点击