代码如下:
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent), ui(new Ui::Dialog)
{
ui->setupUi(this);
tcpSocket = new QTcpSocket( this );
ui->lineEdit->setText("192.168.244.66");
connect( tcpSocket, SIGNAL(error(int)), this, SLOT(errMsg(int)) );
connect( ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked(bool)) );
connect( tcpSocket, SIGNAL(readyRead()), this, SLOT(recvMsg()) );
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButton_clicked(bool checked)
{
int s;
s = tcpSocket->state();
qWarning("state is %d", s);
tcpSocket->connectToHost(QHostAddress(ui->lineEdit->text()), 2010);
QTextStream out(tcpSocket);
out << ui->lineEdit_3->text()<< endl;
}
void Dialog::recvMsg()
{
QString res;
res += tcpSocket->readAll();
ui->textEdit->setText(res);
ui->textEdit->moveCursor(QTextCursor::End);
/* 用了下面这三个函数都不行 */
tcpSocket->disconnectFromHost();
tcpSocket->close();
tcpSocket->abort();
}
void Dialog::errMsg( int errNo )
{
ui->textEdit->setText( "this is err!!!!" );
}