这是在QT6使用老写法报的出错内容:
mainwindow.cpp:52:50: In template: type 'decay_t
' (aka 'MainWindow *') cannot be used prior to '::' because it has no members
qfuture_impl.h:214:49: error occurred here
这是出错语句:
QFuturefuture = QtConcurrent::run(this,&MainWindow::readImage); 正确的语句:
QFuturefuture = QtConcurrent::run(&MainWindow::readImage,this);
QtConcurrent::run()还接受指向成员函数的指针。第一个参数必须是const引用或指向类实例的指针。在调用const成员函数时,传递const引用是有用的;传递指针对于调用修改实例的非const成员函数很有用。
例如,在一个单独的线程中调用QByteArray::split()(一个const成员函数)是这样做的:
- // call 'QList
QByteArray::split(char sep) const' in a separate thread - QByteArray bytearray = "hello world";
- QFuture
> future = QtConcurrent::run(&QByteArray::split, bytearray, ' '); - ...
- QList
result = future.result();
- // call 'void QImage::invertPixels(InvertMode mode)' in a separate thread
- QImage image = ...;
- QFuture<void> future = QtConcurrent::run(&QImage::invertPixels, &image, QImage::InvertRgba);
- ...
- future.waitForFinished();
- // At this point, the pixels in 'image' have been inverted
刚开始没仔细看文档,按老写法一直报错,网上找来找去没什么结果。最后在官网论坛发现留言里的提示,再去看了文档才发现问题。这个问题网上资料挺少提到。希望碰到的朋友能够搜索到这个提示。