Trouble using Qt Socket Programming [SOLVED]

  • Bare in mind, I am a newbie at Qt. My goal is to connect to a server with a certain ip address and wait for data on the port. Once data is available, i want to read the port. Right now alls I am trying to do is create a socket using Qt's programming interface. This is what I have so far.

    @#include <QtGui>
    #include <QtNetwork/QTcpSocket>
    #include <QtNetwork/QAbstractSocket>
    #include "connection.h"
    #include "ui_connection.h"

    // globals
    QTcpSocket tcpSocket;
    QString hostAddress = "";
    unsigned short port = 4395;

    Connection::Connection(QWidget *parent) :
    ui(new Ui::Connection)

    // connect to host using designated port number
    tcpSocket.connectToHost(hostAddress, port, QIODevice::ReadWrite);
    // signals and slots for tcp socket
    connect(tcpSocket, SIGNAL(QTcpSocket::connected()), this, SLOT(socketConnected()));
    connect(tcpSocket, SIGNAL(QTcpSocket::disconnected()), this, SLOT(socketDisconnected()));


    delete ui;

    void Connection::socketConnected()

    void Connection::socketDisconnected()

    After doing this, I get 2 errors in the connect string. Anyone see off the bat what the issue could be? From the tutorials, all i could get from them were, create the socket, connect to host, use connect() with signals and slots to read data. Right now in the above code, alls im seeing is if i am connected or disconnected.

    I feel like this should be simple :/

    Thanks in advance.

  • Moderators

    Try removing the "QTcpSocket::" prefix from connected() and disconnected() in lines 22 and 23.

    Also, as an aside, IMHO the globals in lines 8-10 are kind of icky. Would probably be better style to make them member variables.

  • hmm, i removed them and still the same error. this is the error i am getting, and i believe it has to do with "this" in the connect functions.

    "no matching function for call to 'Connection::connect(QTcpSocket&, const char[13], Connection * const, Qt::connection type'"

  • Moderators

    What does your header for the Connection class look like?

  • Heres my header file code.

    #ifndef CONNECTION_H
    #define CONNECTION_H

    #include <QMainWindow>
    #include <QtGui>
    #include <QtNetwork/QAbstractSocket>

    namespace Ui {
    class Connection;

    class Connection : public QMainWindow

    explicit Connection(QWidget *parent = 0);

    Ui::Connection *ui;

    public slots:
    void socketConnected();

    #endif // CONNECTION_H

  • Moderators

    Oh... the TcpSocket in your connect needs to be a pointer. Try:

    connect(&tcpSocket, SIGNAL(connected()), this, SLOT(socketConnected()));

    Edit: Looks like the @ formatting is shoving a semicolon after the tcpSocket for some reason. It shouldn't be there.

  • Oh wow. Thank you very much! I had a feeling it was something simple :)
    I appreciate your time!

  • Moderators

    No problem!

    If your problem's solved, please edit your original post and change the thread title to "[SOLVED] Trouble using Qt Socket Programming"

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.