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