skumar434
19th February 2009, 15:23
Hi ,
I trying the copy the data read form QtcpSocket to a text edit. I was able to connect to the destination machine , but facing problem in reading. I have just started using QT so please forgive my silly mistakes .
Here is my code
main.cpp
#include <QApplication>
#include "telnetWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
telnetWindow client;
client.show();
return app.exec();
}
telnetWindow.h
#ifndef TELNETWINDOW_H
#define TELNETWINDOW_H
//#include <QProcess>
//#include <QtGui>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QAbstractSocket>
#include "ui_telnet.h"
class telnetWindow : public QWidget,private Ui::telnetWindow
{
Q_OBJECT
public :
telnetWindow(QWidget *parent = 0);
~telnetWindow();
private slots:
void read_data();
void displayError(QAbstractSocket::SocketError socketError);
void enableButton();
private:
QTcpSocket *tcpSocket;
//QTcpSocket tcpSocket2;
// QString currentFortune;
// QString mystring;
// quint16 blockSize;
//void startConsole();
//void exitConsole();
};
#endif
telnetWindow.cpp
#include <QtGui>
#include "telnetWindow.h"
//#include<iostream.h>
//use namespace std;
telnetWindow::telnetWindow(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
tcpSocket = new QTcpSocket(this);
connect(lineEdit_ipAdd, SIGNAL(textChanged(const QString &)),
this, SLOT(enableButton()));
connect(lineEdit_portNo, SIGNAL(textChanged(const QString &)),
this, SLOT(enableButton()));
connect(pushButton_telnet, SIGNAL(clicked()),
this, SLOT(read_data()));
//connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(read_data()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
//textEdit->append("----------------Console Window-----------------------------");
lineEdit_portNo->setFocus();
}
telnetWindow::~telnetWindow()
{};
void telnetWindow::read_data()
{
QByteArray consoleop;
int ret_val;
pushButton_telnet->setEnabled(true);
tcpSocket->abort();
tcpSocket->connectToHost(lineEdit_ipAdd->text(),
lineEdit_portNo->text().toInt(),QIODevice::ReadWrite);
//tcpSocket->write(data);
if(tcpSocket->waitForConnected(-1))
{
//textEdit->append("Console conncted");
//qdebug() << "Connected";
ret_val = tcpSocket->state();
// cout << ret_val;
if(tcpSocket->waitForReadyRead())
{
tcpSocket->flush();
//cout << tcpSocket->waitForReadyRead();
//while(tcpSocket->canReadLine())
// {
//QString text;
//mystring=tcpSocket->readLine();
consoleop = tcpSocket->readAll();
QString text = textEdit->toPlainText()+QString::fromLocal8Bit(consoleop);
//QString text=tcpSocket->readAll();
//textEdit->append(text);
//cout<<text;
textEdit->append(consoleop);
label->setText(text);
textEdit->insert(consoleop);
label->setText(text);
// qdebug() << text;
// }
pushButton_telnet->setEnabled(false);
}
}
}
I simply have a button , line edit and textedit.
When I try to print the output of the
textEdit->append(consoleop); -- Its printing some garbage value..
I think I need to convert data to some type Or ?
Plz help me solving this .....
I trying the copy the data read form QtcpSocket to a text edit. I was able to connect to the destination machine , but facing problem in reading. I have just started using QT so please forgive my silly mistakes .
Here is my code
main.cpp
#include <QApplication>
#include "telnetWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
telnetWindow client;
client.show();
return app.exec();
}
telnetWindow.h
#ifndef TELNETWINDOW_H
#define TELNETWINDOW_H
//#include <QProcess>
//#include <QtGui>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QAbstractSocket>
#include "ui_telnet.h"
class telnetWindow : public QWidget,private Ui::telnetWindow
{
Q_OBJECT
public :
telnetWindow(QWidget *parent = 0);
~telnetWindow();
private slots:
void read_data();
void displayError(QAbstractSocket::SocketError socketError);
void enableButton();
private:
QTcpSocket *tcpSocket;
//QTcpSocket tcpSocket2;
// QString currentFortune;
// QString mystring;
// quint16 blockSize;
//void startConsole();
//void exitConsole();
};
#endif
telnetWindow.cpp
#include <QtGui>
#include "telnetWindow.h"
//#include<iostream.h>
//use namespace std;
telnetWindow::telnetWindow(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
tcpSocket = new QTcpSocket(this);
connect(lineEdit_ipAdd, SIGNAL(textChanged(const QString &)),
this, SLOT(enableButton()));
connect(lineEdit_portNo, SIGNAL(textChanged(const QString &)),
this, SLOT(enableButton()));
connect(pushButton_telnet, SIGNAL(clicked()),
this, SLOT(read_data()));
//connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(read_data()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
//textEdit->append("----------------Console Window-----------------------------");
lineEdit_portNo->setFocus();
}
telnetWindow::~telnetWindow()
{};
void telnetWindow::read_data()
{
QByteArray consoleop;
int ret_val;
pushButton_telnet->setEnabled(true);
tcpSocket->abort();
tcpSocket->connectToHost(lineEdit_ipAdd->text(),
lineEdit_portNo->text().toInt(),QIODevice::ReadWrite);
//tcpSocket->write(data);
if(tcpSocket->waitForConnected(-1))
{
//textEdit->append("Console conncted");
//qdebug() << "Connected";
ret_val = tcpSocket->state();
// cout << ret_val;
if(tcpSocket->waitForReadyRead())
{
tcpSocket->flush();
//cout << tcpSocket->waitForReadyRead();
//while(tcpSocket->canReadLine())
// {
//QString text;
//mystring=tcpSocket->readLine();
consoleop = tcpSocket->readAll();
QString text = textEdit->toPlainText()+QString::fromLocal8Bit(consoleop);
//QString text=tcpSocket->readAll();
//textEdit->append(text);
//cout<<text;
textEdit->append(consoleop);
label->setText(text);
textEdit->insert(consoleop);
label->setText(text);
// qdebug() << text;
// }
pushButton_telnet->setEnabled(false);
}
}
}
I simply have a button , line edit and textedit.
When I try to print the output of the
textEdit->append(consoleop); -- Its printing some garbage value..
I think I need to convert data to some type Or ?
Plz help me solving this .....