Lambda 介绍
Lambda 函数也叫匿名函数, 是C++ 11中新增的特性;谁用谁知道,哈;
- Lambda函数的好处
Lambda函数使代码变得更加紧凑、更加结构化和更富有表现力;
lambda在c++作用主要是作为内嵌函数,逻辑更加清楚,代码可读性更好;
【QT】Qt之QFutureWatcher
简述
QFuture 表示异步计算的结果 ,QFutureWatcher 则允许使用信号和槽监视 QFuture,也就是说,QFutureWatcher 是为 QFuture 而生的。
// 实例化对象,并连接到 finished() 信号。
MyClass myObject;
QFutureWatcher<int> watcher;
connect(&watcher, SIGNAL(finished()), &myObject, SLOT(handleFinished()));
// 开始计算
QFuture<int> future = QtConcurrent::run(...);
watcher.setFuture(future);
使用 lambda
if(nullptr == WidgetWatcher)
{
WidgetWatcher = new QFutureWatcher<void>;
connect(WidgetWatcher, &QFutureWatcher<void>::finished, this, [=]() {
qDebug()<<"finish threadid "<<QThread::currentThreadId();
mQProgressDialog->close();
});
}
QFuture<void> future = QtConcurrent::run([=]() {
//slotShowInsertionWidget();
qDebug()<<"111 threadid "<<QThread::currentThreadId();
QThread::sleep(3);
getTextStaticFun();
});
WidgetWatcher->setFuture(future);
传参:
// 传入 当前 this
QFuture<void> future = QtConcurrent::run([&]()
还可以使用 QProgressDialog 作为阻堵 函数,变成同步;
progress->exec();
完成后,关闭;
connect(WidgetWatcher, &QFutureWatcher<void>::finished, this, [=]() {
qDebug()<<"finish threadid "<<QThread::currentThreadId();
mQProgressDialog->close();
});
MyQProgressDialog
#ifndef MYPROGRESSDIALOG_H
#define MYPROGRESSDIALOG_H
#include<QProgressDialog>
class MyProgressDialog :public QProgressDialog
{
Q_OBJECT
public:
MyProgressDialog(const QString &labelText);
};
#include "myprogressdialog.h"
#include<QProgressBar>
MyProgressDialog::MyProgressDialog(const QString &labelText)
{
setAutoClose(false); //进度达到最大值时不关闭,默认为true
// setLabelText("正在导出数据,请稍后..."); //显示的文本
setLabelText(labelText);
setCancelButton(NULL); //不显示取消按钮
setWindowModality(Qt::WindowModal);
//mQProgressDialog->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog | Qt::WindowStaysOnTopHint);
setMinimumWidth(600);
// setWindowModality(Qt::ApplicationModal);
//setModal(true);
QProgressBar * prog = new QProgressBar(this);
prog->setTextVisible(false);
//prog->setStyleSheet("QProgressBar{ max-height: 20px;\nbackground: #323232;\nborder-radius: 10px;\ntext-align:center;\n}\nQProgressBar::chunk { background: #2C6ED5;\nborder-radius: 10px;\n}");
prog->setRange(0, 0);
setBar(prog);
//mQProgressDialog->hide();
reset();
}