首页 > 解决方案 > QObject::connect has too many arguments

问题描述

So I'm having trouble getting connect() to work. Based on QT documentation I can't see anything that's obviously wrong with my code.

I'm not at liberty to share the rest of the code in this project, and I don't think it would be relevant anyway, so I'm just going to provide my main function along with the class header file in which my close_thread slot exists. This seems to be the only place where the problem persists.

Basically, inside the worker thread I create in main() there's a while loop doing a bunch of dirty work. That's not terribly important to my question though.

What I'm currently encountering is that when I close the application, the thread is still running and everything freaks out. My idea then was to send a signal right before the app is closed that allows the thread to return from its function and close before run time ends, thus avoiding the problems I described.

As of right now trying to connect the aboutToQuit signal in my QApplication with my slot close_thread produces multiple errors, which I've listed below.

I've used connect() in other places throughout the project with no problems whatsoever so I'm kind of confused by this particular hiccup.

 int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);

     MainWindow w;
     WorkerThread worker(w.ui);
     connect(&a, &QApplication::aboutToQuit, &worker, &WorkerThread::close_thread);

     w.show();
     worker.start();

     return a.exec();

}

Error message:

invalid conversion from 'QApplication*' to 'SOCKET {aka unsigned int}' [-fpermissive] connect(&a, &QApplication::aboutToQuit, &worker, &WorkerThread::close_thread);


cannot convert 'void (QCoreApplication::)(QCoreApplication::QPrivateSignal)' to 'const sockaddr' for argument '2' to 'int connect(SOCKET, const sockaddr*, int)'

Any help would be much appreciated. Thanks for you time!

标签: c++multithreadingqt

解决方案


问题是由重叠名称引起的,您确定要导入以下标头:

#include <sys/types.h>
#include <sys/socket.h>

或包含这些标头的标头,并且您正在使用该标头中定义的连接。

必须使用 QObject 的 connect 方法:

QObject::connect(&a, &QApplication::aboutToQuit, &worker, &WorkerThread::close_thread);

推荐阅读