在模板中:static_assert 由于 qt c++ 中的要求而失败
In template: static_assert failed due to requirement in qt c++
我正在 QT c++ 上制作应用程序。这是一个问题。当我尝试在 QT 中创建 Future 时,出现了很多错误。这是我的一些代码:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
void SomeFunc(QString path, QString name, QString result, bool isProgressBar);
};
#endif // MAINWINDOW_H
mainwindow.cpp
void MainWindow::SomeFunc(QString path, QString name, QString result, bool isProgressBar)
{
}
void MainWindow::on_pushButton_3_clicked()
{
auto futureWatcher = new QFutureWatcher<void>(this);
QObject::connect(futureWatcher, &QFutureWatcher<void>::finished, futureWatcher, &QFutureWatcher<void>::deleteLater);
auto future = QtConcurrent::run( &MainWindow::SomeFunc, file_path, file_name, result_name, isProgressBar );
futureWatcher->setFuture(future);
}
这是错误:
In template: static_assert failed due to requirement
'QtPrivate::ArgResolver<void (MainWindow::*)(QString, QString, QString, bool)>::integral_constant<bool, false>::value' "The first argument of passed callable object isn't a QPromise<T> & type. Did you intend to pass a callable which takes a QPromise<T> & type as a first argument? Otherwise it's not possible to invoke the function with passed arguments."
qtconcurrentstoredfunctioncall.h:225:5: error occurred here
qtconcurrentstoredfunctioncall.h:248:18: in instantiation of template class 'QtConcurrent::PromiseTaskResolver<void (MainWindow::*)(QString, QString, QString, bool), QString, QString, QString, bool>' requested here
qtconcurrentstoredfunctioncall.h:253:30: in instantiation of template class 'QtConcurrent::TaskResolverHelper<std::integral_constant<bool, false>, void (MainWindow::*)(QString, QString, QString, bool), QString, QString, QString, bool>' requested here
qtconcurrentrun.h:76:12: in instantiation of template class 'QtConcurrent::TaskResolver<void (MainWindow::*)(QString, QString, QString, bool), QString, QString, QString, bool>' requested here
qtconcurrentrun.h:93:12: in instantiation of function template specialization 'QtConcurrent::run<void (MainWindow::*)(QString, QString, QString, bool), QString &, QString &, QString &, bool &>' requested here
mainwindow.cpp:163:33: in instantiation of function template specialization 'QtConcurrent::run<void (MainWindow::*)(QString, QString, QString, bool), QString &, QString &, QString &, bool &>' requested here
有人知道如何解决这个问题吗?
auto future = QtConcurrent::run( &MainWindow::SomeFunc, file_path, file_name, result_name, isProgressBar );
当看到任何涉及指向成员函数的指针的代码行时,一个直接的问题就会浮现在脑海中:“正在调用方法的哪个实例?”
在这种情况下,none,显然有问题。我们需要以某种方式提供该信息。
根据QtConcurrent::run
的文档,有专门的语法来处理这种情况,它要求将目标对象作为第一个参数传递。
具体来说,这意味着您实际上调用了错误的函数重载。编译器尽最大努力生成被调用代码的正面或反面,但由于解释了错误的代码,它最终会因一些看似无关的错误而失败。换句话说,你得到的错误是由于编译器 confused.
根据文档,代码应如下所示:
QtConcurrent::run(this, &MainWindow::SomeFunc, file_path, file_name, result_name, isProgressBar );
但是,正确解释该行代码需要知道、学习或猜测 QtConcurrent::run
的重载解析是如何工作的,这不是很好。
Lambdas 让你做同样的事情(在我看来)对未来的认知负担要少得多 reader:
QtConcurrent::run([=]{
SomeFunc(file_path, file_name, result_name, isProgressBar );
});
我正在 QT c++ 上制作应用程序。这是一个问题。当我尝试在 QT 中创建 Future 时,出现了很多错误。这是我的一些代码:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
void SomeFunc(QString path, QString name, QString result, bool isProgressBar);
};
#endif // MAINWINDOW_H
mainwindow.cpp
void MainWindow::SomeFunc(QString path, QString name, QString result, bool isProgressBar)
{
}
void MainWindow::on_pushButton_3_clicked()
{
auto futureWatcher = new QFutureWatcher<void>(this);
QObject::connect(futureWatcher, &QFutureWatcher<void>::finished, futureWatcher, &QFutureWatcher<void>::deleteLater);
auto future = QtConcurrent::run( &MainWindow::SomeFunc, file_path, file_name, result_name, isProgressBar );
futureWatcher->setFuture(future);
}
这是错误:
In template: static_assert failed due to requirement
'QtPrivate::ArgResolver<void (MainWindow::*)(QString, QString, QString, bool)>::integral_constant<bool, false>::value' "The first argument of passed callable object isn't a QPromise<T> & type. Did you intend to pass a callable which takes a QPromise<T> & type as a first argument? Otherwise it's not possible to invoke the function with passed arguments."
qtconcurrentstoredfunctioncall.h:225:5: error occurred here
qtconcurrentstoredfunctioncall.h:248:18: in instantiation of template class 'QtConcurrent::PromiseTaskResolver<void (MainWindow::*)(QString, QString, QString, bool), QString, QString, QString, bool>' requested here
qtconcurrentstoredfunctioncall.h:253:30: in instantiation of template class 'QtConcurrent::TaskResolverHelper<std::integral_constant<bool, false>, void (MainWindow::*)(QString, QString, QString, bool), QString, QString, QString, bool>' requested here
qtconcurrentrun.h:76:12: in instantiation of template class 'QtConcurrent::TaskResolver<void (MainWindow::*)(QString, QString, QString, bool), QString, QString, QString, bool>' requested here
qtconcurrentrun.h:93:12: in instantiation of function template specialization 'QtConcurrent::run<void (MainWindow::*)(QString, QString, QString, bool), QString &, QString &, QString &, bool &>' requested here
mainwindow.cpp:163:33: in instantiation of function template specialization 'QtConcurrent::run<void (MainWindow::*)(QString, QString, QString, bool), QString &, QString &, QString &, bool &>' requested here
有人知道如何解决这个问题吗?
auto future = QtConcurrent::run( &MainWindow::SomeFunc, file_path, file_name, result_name, isProgressBar );
当看到任何涉及指向成员函数的指针的代码行时,一个直接的问题就会浮现在脑海中:“正在调用方法的哪个实例?”
在这种情况下,none,显然有问题。我们需要以某种方式提供该信息。
根据QtConcurrent::run
的文档,有专门的语法来处理这种情况,它要求将目标对象作为第一个参数传递。
具体来说,这意味着您实际上调用了错误的函数重载。编译器尽最大努力生成被调用代码的正面或反面,但由于解释了错误的代码,它最终会因一些看似无关的错误而失败。换句话说,你得到的错误是由于编译器 confused.
根据文档,代码应如下所示:
QtConcurrent::run(this, &MainWindow::SomeFunc, file_path, file_name, result_name, isProgressBar );
但是,正确解释该行代码需要知道、学习或猜测 QtConcurrent::run
的重载解析是如何工作的,这不是很好。
Lambdas 让你做同样的事情(在我看来)对未来的认知负担要少得多 reader:
QtConcurrent::run([=]{
SomeFunc(file_path, file_name, result_name, isProgressBar );
});