PDA

View Full Version : connecting QTcpSocket::readyRead() to the slot QTextArea::append()



OriginalCopy
1st November 2007, 07:55
Incoming data and the end of the pipe is not appended to the textarea and I don't see any mistake in the code. Could you give me a hand please ?


//******************** The declaration file
#ifndef IRC_CONNECTION_H_
# define IRC_CONNECTION_H_
# include <QTcpSocket>
class QTcpSocket;

class QBuffer;

class IrcConnection : public QTcpSocket {
Q_OBJECT

public:
IrcConnection(QTcpSocket *parent = 0);
~IrcConnection();
signals:
void incomingIrcMessage(QString &);
private slots:
void receiveMessage();
void ircRegister();
private:
QBuffer *buffer;
};

#endif //IRC_CONNECTION_H_

//******************** The implementation file

#include "IrcConnection.h"
#include <QBuffer>
#include <QTcpSocket>
#include <QString>
#include <QObject>

IrcConnection::IrcConnection(QTcpSocket *parent) : QTcpSocket(parent) {
buffer = new QBuffer(this);
QObject::connect(this,SIGNAL(readyRead()),this,SLO T(receiveMessage()));
QObject::connect(this,SIGNAL(connected()),this,SLO T(ircRegister()));
}

IrcConnection::~IrcConnection() {
//TODO cleanup
}
void IrcConnection::receiveMessage() {
qint64 bytes = buffer->write(readAll());
buffer->seek(buffer->pos() - bytes);
while(buffer->canReadLine()) {
QString line = buffer->readLine();
emit incomingIrcMessage(line);
}
}
void IrcConnection::ircRegister() {
write("NICK kdsjghsdkjghsdkjgnUSER "anonymous.com" "irc.undernet.org" :http://localhostn");
}

//******************** main.cpp
#include <QApplication>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QtGlobal>
#include "IrcConnection.h"

int main(int argc, char *argv[]) {
QApplication app(argc,argv);
QVBoxLayout *vbox = new QVBoxLayout;
QTextEdit *textarea = new QTextEdit;
vbox->addWidget(textarea);
textarea->setReadOnly(true);

IrcConnection *irc = new IrcConnection;

QObject::connect(irc,SIGNAL(incomingIrcMessage(QSt ring &)),textarea,SLOT(append(QString &)));
QString host("irc.freenode.org");
qint16 port = 6667;
textarea->append("Connecting...n");
qDebug("Connecting...");
irc->connectToHost(host,port);
textarea->show();
return app.exec();
}

OriginalCopy
1st November 2007, 14:09
problem solved, the SLOT and SIGNAL were having different stack signatures