my-QT专栏

QtConcurrent

2021-08-22  本文已影响0人  c之气三段
QT += concurrent
//无法触发弹窗控件等,可通过信号的形式触发无法使用的功能
 QtConcurrent::run([=](){
        qDebug() << __FUNCTION__  << QThread::currentThreadId() << QThread::currentThread();
    });
QThreadPool pool;
QtConcurrent::run(&pool, func);

数据传递

当匿名函数[=]时可捕捉到外部变量,我尝试了传控件不行。
内部变量传出来

int id=5;
QtConcurrent::run([=](){
            foreach (auto var, m_mapFileData)
            {
                if(var->getId()==id)
                {
                    QString filePath;
                    if(!var->getPath().contains(":"))
                    {
                        filePath.append(QDir::currentPath()).append("/");
                    }
                    filePath.append(var->getPath());
                    QFile file(filePath);
                    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
                    {
                        QTextStream in(&file);
                        QVector<QString> vect;
                        qRegisterMetaType< QVector<QString>>("QVector<QString>&");//注册此变量到线程
                        while(!in.atEnd())
                        {
                            QString strLine=in.readLine();
                            vect.append(strLine);
                        }
                        file.close();
                        emit signal_fillPerview(vect);//通过信号将数据传出
                    }
                }
            }
           });
上一篇 下一篇

猜你喜欢

热点阅读