简体   繁体   English

在Qt5中未连接connect(QObject *,SIGNAL(signal()),functor)

[英]connect(QObject*, SIGNAL(signal()), functor) not connecting in qt5

I am attempting to connect a signal from a QObject to a functor as defined in the QT5 documentation: 我正在尝试按照QT5文档中的定义将信号从QObject连接到函子:

http://doc.qt.io/qt-5/qobject.html#connect-5 http://doc.qt.io/qt-5/qobject.html#connect-5

Relevant code: 相关代码:

namespace someNamespace
{
void processFinished()
{
    qDebug() << "A Thread has finished processing!!!";
}

void executeWorkerOnSeparateTread()
{
    QThread* thread = new QThread;
    Worker* worker = new Worker();
    worker->moveToThread(thread);
    QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    QObject::connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
    QObject::connect(worker, SIGNAL(finished()), processFinished); //DOES NOT COMPILE
    thread->start();
}
}

If I were to comment out just the line that does not compile, this code compiles and executes with no problems. 如果我只注释掉未编译的行,则此代码可以编译并执行而不会出现问题。 I just can't get that connection to compile. 我就是无法编译该连接。

Compiler Error: 编译器错误:

no matching function for call to 'QObject::connect(Worker*&, const char*, void(&)())'

of course Worker inherits QObject and has the Q_OBJECT keyword in it. 当然,Worker继承了QObject并具有Q_OBJECT关键字。 the fact that this works without the problematic line also eliminates other lines being the problem. 没有问题线的情况也可以消除其他问题。

Any ideas as to what I'm doing wrong?? 关于我在做什么错的任何想法吗? It seems to me like it should be this straightforward. 在我看来,这应该很简单。

If relevant, QT version is 5.8 and GCC version is 4.8.5. 如果相关,QT版本为5.8,GCC版本为4.8.5。

Thanks!! 谢谢!!

That's correct, there is no way to connect to a function in a namespace or a lambda using the moc -based signals and slots. 没错,无法使用基于moc的信号和插槽连接到名称空间或lambda中的函数。 Have a look at the list of overloads for QObject::connect()` to see why the error is reported the way it is. 查看QObject :: connect()`的重载列表,以了解为什么以这种方式报告错误。 The correct function to use here is connect overload for a functor and it uses a pointer to a member function as its first argument: 此处使用的正确函数是函子的connect重载 ,它使用指向成员函数的指针作为其第一个参数:

QMetaObject::Connection
QObject::connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)

Thus, use: 因此,使用:

QObject::connect(worker, &Worker::finished, processFinished);

Note the lack of round braces after function names - being careless with that caused me some headache back in the days. 请注意,函数名称后没有圆括号-粗心的做法让我回想起来。

That also means you may want to switch to the new syntax for uniformity sake. 这也意味着您可能需要出于统一性而切换到新语法。

Refer to part 1 and part 2 of the blog post series by Olivier Goffart, the author of the new signal and slot syntax, for more details. 有关更多详细信息,请参阅新信号和插槽语法的作者Olivier Goffart的博客文章系列的第1部分第2部分 Also see a detailed side-by-side rundown of the two approaches here . 另请参阅此处的两种方法的详细并排概要。

Use the new compile-time-checked connect syntax. 使用新的经过编译时检查的连接语法。 You are using a new enough Qt that it's available. 您正在使用足够新的Qt。 Will get rid of the ugly SIGNAL and SLOT macros and will give you compile time errors rather than run time errors when a connection cannot be made and this will work with a lambda and member function and most other callables as well. 当无法建立连接时,它将摆脱丑陋的SIGNALSLOT宏,并为您提供编译时错误,而不是运行时错误,这将适用于lambda和成员函数以及大多数其他可调用对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM