2
votes

When I try to connect using the old signal/slot mechanism, it works fine, but it gives me a compile error using the new one:

// Old mechanism, this works:
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
// Compile error when using the template version:
connect(socket, &QTcpSocket::error, this, &MainWindow::onError);

This is the error I get:

error: no matching function for call to 'MainWindow::connect(QTcpSocket*&, , MainWindow*, void (MainWindow::*)(QAbstractSocket::SocketError))'
         connect(socket, &QTcpSocket::error, this, &MainWindow::onError);
                                                                       ^

My slot function:

class MainWindow : public QMainWindow
{
    Q_OBJECT
private slots:
    void onError(QAbstractSocket::SocketError);

I found a similar thread on Qt forums, and they say it's a bug in Qt but would be fixed by 5.1. My version is 5.4.2, however (using MinGW).

So is it a Qt bug for real, or is my syntax wrong?

2

2 Answers

2
votes

You had the right link to the Qt forums, but read the wrong part. Look for static_cast on this page.

connect (socket, static_cast<void (QTcpSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error), this, &MainWindow::onError);

This (not very elegant) cast is necessary because the method name "error" is ambiguous.

1
votes

Your link is wrong, reason is that MOC compiler don't know with one should use. If there are 2 same name signals but different params, MOC is confused with one you mean.

That why sometimes is better to use 'old' syntax than new one.

From doc you can find same function as good example of that:

error() const : SocketError
error(QAbstractSocket::SocketError)

With one MOC should use? :)

Edit: I know that one of them is signal, other is normal function, but from MOC point of view, this don't matter, both are 'functions' from C++ point of view. Only difference is 'generate c++ code from MOC' (as signals/slots are 'created by moc compiler')