本文介绍了Qt连接函数 - 使用lambda的信号消歧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用c ++ 11连接语法,并使用此连接语句得到以下错误:

I'm using the c++11 connect syntax, and get the following error with this connect statement:

connect(fileSystemCompleter, &QCompleter::activated, [&] (QModelIndex index)
{
    fileSystemPathEdit->setFocus(Qt::PopupFocusReason);
});

错误:

error C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const' : cannot convert parameter 2 from 'overloaded-function' to 'const char *'
Context does not allow for disambiguation of overloaded function

重写这种方式,以便编译器可以消除重载的函数?

Is it possible to rewrite this somehow so that the compiler CAN disambiguate the overloaded function?

编辑:

来自Qt项目...

重载

在示例中,连接到QAbstractSocket :: error
不是真的漂亮,因为错误有一个重载,并且重载函数的
地址需要显式转换。

As you might see in the example, connecting to QAbstractSocket::error is not really beautiful since error has an overload, and taking the address of an overloaded function requires explicit casting.

一些宏可以帮助(使用c ++ 11或typeof扩展)

Some macro could help (with c++11 or typeof extensions)

最好的事情可能是建议不要超载信号或
slot ...

The best thing is probably to recommend not to overload signals or slots …

...但是我们在过去的小版本的Qt
中添加了重载,因为获取函数的地址不是一个用例we bb $ b支持。但现在这是不可能的,没有打破来源
兼容性。

… but we have been adding overloads in past minor releases of Qt because taking the address of a function was not a use case we support. But now this would be impossible without breaking the source compatibility.

任何想法这个宏将是什么样子?

Any ideas what exactly this macro would look like? Or how to do the explicit casting?

推荐答案

您必须显式强制转换重载指针:

You have to explicitly cast the overload pointer:

void (QCompleter::* activatedOverloadPtr)(const QModelIndex&) = &QCompleter::activated;
connect(fileSystemCompleter, activatedOverloadPtr, [&] (QModelIndex index)
{
    fileSystemPathEdit->setFocus(Qt::PopupFocusReason);
});

这篇关于Qt连接函数 - 使用lambda的信号消歧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:57