PDA

View Full Version : QTcpSocket



Fallen_
24th August 2010, 20:31
Hi, can someone help me with this code?
I don't know if it connects or anything it just doesnt write anything to the "QTextEdit* read"
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QDialog>
#include <QTcpSocket>

class QLineEdit;
class QLabel;
class QPushButton;
class QTextEdit;

class MainWindow : public QDialog
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0);

public slots:
void Connect();
void appendToWindow();
void sendMessage();

private:
QLabel *ipLabel, *portLabel;
QPushButton *done, *quit;
QLineEdit *ipLine, *portLine, *write;
QTcpSocket *socket;
QTextEdit *read;
};
#endif
mainwindow.cpp

#include "mainwindow.h"
#include <QtGui>
#include <QtNetwork>

MainWindow::MainWindow(QWidget* parent)
: QDialog(parent)
{
socket = new QTcpSocket(this);
ipLabel = new QLabel(tr("<i><font color=red>IP:</font></i>"));
portLabel = new QLabel(tr("<i><font color=red>Port:</font></i>"));
done = new QPushButton(tr("Connect"));
quit = new QPushButton(tr("Quit"));
ipLine = new QLineEdit;
portLine = new QLineEdit;
write = new QLineEdit;
read = new QTextEdit;

read->setReadOnly(true);

QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(ipLabel);
layout->addWidget(ipLine);
layout->addWidget(portLabel);
layout->addWidget(portLine);

QVBoxLayout* layout2 = new QVBoxLayout;
layout2->addWidget(done);
layout2->addWidget(quit);

QVBoxLayout* layout3 = new QVBoxLayout;
layout3->addWidget(write);
layout3->addWidget(read);

QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addLayout(layout);
mainLayout->addLayout(layout2);
mainLayout->addLayout(layout3);
setLayout(mainLayout);

connect(done, SIGNAL(clicked()), this, SLOT(Connect()));
connect(quit, SIGNAL(clicked()), this, SLOT(close()));
connect(done, SIGNAL(clicked()), this, SLOT(appendToWindow()));
connect(write, SIGNAL(textChanged(const QString&)), this, SLOT(sendMessage()));
}

void MainWindow::Connect()
{
qint16 port;
QString host;
if(ipLine->text().isEmpty())
return;

if(portLine->text().isEmpty())
port = 6667;

host = ipLine->text();
socket->connectToHost(host, port);
}

void MainWindow::appendToWindow()
{
char buffer[1024];
while(socket->canReadLine()) {
socket->readLine(buffer, sizeof(buffer));
read->append(buffer);
}
}

void MainWindow::sendMessage()
{
if(write->text().isEmpty())
return;

socket->write(write->text().toLatin1());
}
main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.resize(200, 200);
return a.exec();
}

Any help is appreciated.

hkaraoguz84
24th August 2010, 22:00
Hi, can someone help me with this code?
I don't know if it connects or anything it just doesnt write anything to the "QTextEdit* read"
mainwindow.h




connect(write, SIGNAL(textChanged(const QString&)), this, SLOT(sendMessage()));
}




Hi,

This part is problemmatic I guess. The signal and corresponding slot should have the same arguments. The connection should be like




connect(write, SIGNAL(textChanged(QString)), this, SLOT(sendMessage(QString)));




Your slot should be declared as

void sendMessage(const QString& dummy)

I hope this solves your problem.

hkaraoguz84
24th August 2010, 22:17
The reason you cannot type to "TextEdit* read" is





read->setReadOnly(true);


if you change the argument to "false", you will be able to write in the "read".

Fallen_
24th August 2010, 22:50
Hi,

This part is problemmatic I guess. The signal and corresponding slot should have the same arguments. The connection should be like




connect(write, SIGNAL(textChanged(QString)), this, SLOT(sendMessage(QString)));




Your slot should be declared as

void sendMessage(const QString& dummy)

I hope this solves your problem.
my problem is that im not receiving anything from the socket & i need to be able to receive & send, this function:

void MainWindow::appendToWindow()
{
char buffer[1024];
while(socket->canReadLine()) {
socket->readLine(buffer, sizeof(buffer));
read->append(buffer);
}
}

The reason you cannot type to "TextEdit* read" is


if you change the argument to "false", you will be able to write in the "read".
i don't want to write here, this text edit is for receiving data from the socket ;/
--
edit:
i made something like this:

if(socket->waitForConnected(-1) == true)
read->append("Connected.");
else
read->append(socket->errorString());
it gave me this:

Socket operation timed out

Fallen_
25th August 2010, 22:40
bump.........

tbscope
26th August 2010, 05:32
The textChanged signal is fired every time you press a key. This might not be the behaviour you want.

Use the readyRead() signal of the socket and connect it to your appendToWindow() slot.

By the way, if a socket times out, that's in most cases not a problem with your code.