Home > other >  In template: static_assert failed due to requirement in qt c
In template: static_assert failed due to requirement in qt c

Time:04-14

I`m making an app on QT c . And here is a problem. When I try to create Future in QT, I got many errors. Here is a bit of my code:

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);
}

Here is error:

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

Does anyone know how this can be fixed?

CodePudding user response:

auto future = QtConcurrent::run( &MainWindow::SomeFunc, file_path, file_name, result_name, isProgressBar );

An immediate question comes to mind when seeing any line of code involving a pointer to a member function: "Which instance of MainWindow is the method being called on?"

In this case, none, so there's obviously a problem. We need to provide that information somehow.

According to the documentation of QtConcurrent::run, there is a dedicated syntax to handle that case, which requires the target object to be passed as the first argument.

Concretely, this means that you are effectively calling the wrong function overload, and any error you are getting downstream from that are likely to be ultimately due to the compiler being confused.

Following the documentation, the code should look like this:

QtConcurrent::run(this, &MainWindow::SomeFunc, file_path, file_name, result_name, isProgressBar );

However, interpreting that line of code correctly requires either knowing, learning or guessing how the overload resolution of QtConcurrent::run works, which is not great.

Lambdas let you do the same thing with (in my opinion) a lot less cognitive load for a future reader:

QtConcurrent::run([=]{
 SomeFunc(file_path, file_name, result_name, isProgressBar );
});
  • Related