Question

I'm coming from Objective-C and love that inline functions have come to c++ in c++11, they're useful! Sadly though, I'm having a problem here with my slot (this setup reminds me of protocols and delegates a little bit, I am wondering if I could create a class specifically to behave like a "delegate" like in obj-c).

#include <QCoreApplication>
#include <QtNetwork/QTcpSocket>

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

    QTcpSocket *socket = new QTcpSocket;
    socket->connectToHost("qt.nokia.com", 80);
    QObject::connect(socket, &QTcpSocket::connected, [socket, page] () {
        socket->write(QByteArray("GET " + "index.html" + "\r\n"));
    });
    QObject::connect(socket, &QTcpSocket::readyRead, [socket] () {
        qDebug()<< "GOT DATA "<< socket->readAll();
    });

    return a.exec();

}

This gives me the following error:

main.cpp:10: error: expected expression
QObject::connect(socket, &QTcpSocket::connected, [socket, page] () {
                             ^
Was it helpful?

Solution

This had to be added to my .pro file:

CONFIG += c++11

I guess although QT 5 supports a new SIGNAL/SLOT overload for lambda-functions, it still doesn't use c++11 by default. Odd, I wonder why.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top