Trouble using Qt Socket Programming [SOLVED]
-
wrote on 18 Jul 2011, 17:21 last edited by
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.
-
wrote on 18 Jul 2011, 18:28 last edited by
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.
-
wrote on 18 Jul 2011, 18:41 last edited by
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'"
-
wrote on 18 Jul 2011, 18:43 last edited by
What does your header for the Connection class look like?
-
wrote on 18 Jul 2011, 18:46 last edited by
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
@ -
wrote on 18 Jul 2011, 18:48 last edited by
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.
-
wrote on 18 Jul 2011, 18:49 last edited by
Oh wow. Thank you very much! I had a feeling it was something simple :)
I appreciate your time! -
wrote on 18 Jul 2011, 21:33 last edited by
No problem!
If your problem's solved, please edit your original post and change the thread title to "[SOLVED] Trouble using Qt Socket Programming"
1/8