В качестве учебного примера я пытаюсь протестировать std :: thread с Qt вместо QThreads. Это очень простое приложение c QMainWindow, см. Код ниже:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
[[ noreturn ]] void operator()();
private:
Ui::MainWindow *ui;
QString mm;
};
#endif // MAINWINDOW_H
mainwindow. cpp
#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
[[ noreturn ]] void MainWindow::operator()()
{
qDebug()<< "thread runing";
int i =0;
while (1)
{
i++;
}
}
main. cpp
#include <thread>
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
std::thread t(&mainWindow);
t.detach();
return app.exec();
}
Этот код не компилируется и выдает ошибку:
In file included from /home/faroub/Documents/development-projects/projects-c++/Qt-CMake-GUI/HelloWorld/main.cpp:1:0:
/usr/include/c++/7/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<MainWindow*> >’:
/usr/include/c++/7/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = MainWindow*; _Args = {}]’
/home/faroub/Documents/development-projects/projects-c++/Qt-CMake-GUI/HelloWorld/main.cpp:10:30: required from here
/usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<MainWindow*> >::_M_invoke(std::thread::_Invoker<std::tuple<MainWindow*> >::_Indices)’
operator()()
^~~~~~~~
Any идеи почему это не работает? Должен ли я делать QThreads с Qt? это связано с QObject? заранее спасибо.