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 = "127.0.0.1";
unsigned short port = 4395;Connection::Connection(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Connection)
{
ui->setupUi(this);// 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()));
}
Connection::~Connection()
{
delete ui;
}void Connection::socketConnected()
{
ui->txttcpstate->setText("Connected");
}void Connection::socketDisconnected()
{
ui->txttcpstate->setText("Disconnected");
}@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.
-
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'"
-
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
{
Q_OBJECTpublic:
explicit Connection(QWidget *parent = 0);
~Connection();private:
Ui::Connection *ui;public slots:
void socketConnected();
};#endif // CONNECTION_H
@ -
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.