Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt connect function - signal disambiguation using lambdas [duplicate]

Tags:

c++

c++11

lambda

qt

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:

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?

EDIT:

From Qt Project...

Overload

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.

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

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

… 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?

like image 812
Nicolas Holthaus Avatar asked Sep 26 '14 14:09

Nicolas Holthaus


1 Answers

You have to explicitly cast the overload pointer:

void (QCompleter::* activatedOverloadPtr)(const QModelIndex&) = &QCompleter::activated;
connect(fileSystemCompleter, activatedOverloadPtr, [&] (QModelIndex index)
{
    fileSystemPathEdit->setFocus(Qt::PopupFocusReason);
});
like image 53
cmannett85 Avatar answered Sep 28 '22 06:09

cmannett85