📄 qjmnet.cpp
字号:
#include "qjmnet.h"#include <qmessagebox.h>//tony#include <QtGui>#include <QtNetwork>QjmnetBase::QjmnetBase( QWidget *parent, Qt::WFlags f ) : QWidget( parent, f ){ setupUi( this );}QjmnetBase::~QjmnetBase(){}/* * Constructs a Example which is a child of 'parent', with the * name 'name' and widget flags set to 'f' */Qjmnet::Qjmnet( QWidget *parent, Qt::WFlags f ) : QjmnetBase( parent, f ){ //tony m_portNum->setRange(1025,65535); m_portNum->setValue(4000); tcpSocket = new QTcpSocket(this); //非常重要的一行代码 connect(m_quitPb, SIGNAL(clicked()), this, SLOT(goodBye())); connect(m_aboutPb, SIGNAL(clicked()), this, SLOT(showAboutMsg())); connect(m_ipaddrLe, SIGNAL(textChanged(const QString &)), this, SLOT(enableConnectButton())); //IP地址栏有变化就发射信号 connect(m_portNum, SIGNAL(textChanged(const QString &)), this, SLOT(enableConnectButton())); connect(m_inputLe, SIGNAL(textChanged(const QString &)), this, SLOT(enableSendMessage())); connect(m_clearPb, SIGNAL(clicked()), m_output_textEdit, SLOT(clear())); //清除输出框 //下面为socket口连接代码 connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readServerData())); //从服务器接受接受消息 connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnectServer())); connect(m_connectPb, SIGNAL(clicked()), this, SLOT(connectServer())); connect(m_disconnectPb, SIGNAL(clicked()), this, SLOT(disconnectServer())); connect(m_inputSendPb, SIGNAL(clicked()), this, SLOT(hasConnected())); //发送消息 }/* * Destroys the object and frees any allocated resources */Qjmnet::~Qjmnet(){ // no need to delete child widgets, Qt does it all for us}/* * A simple slot... not very interesting. */void Qjmnet::goodBye(){ close();}void Qjmnet::showAboutMsg(){ QMessageBox::about(this, tr("About QJMNET"), tr("<font color=#ff0000><strong>This is QJMNET 0.1</strong></font><br> (c)2004-2007 Brillian Network & Automation Interated System Co.,Ltd. <br>Author: Eric Qiu <jm_qiu@sohu.com>"));}///tonyQString Qjmnet::getHost(){ return m_ipaddrLe->text();}int Qjmnet::getDIPort(){ return m_portNum->value();}void Qjmnet::setDIPort(int port){ m_portNum->setValue(port);}///////////////////////////////////////////////////////////////////void Qjmnet::enableConnectButton() //容许连接按钮{ m_connectPb->setEnabled(!m_ipaddrLe->text().isEmpty() && !m_portNum->text().isEmpty());}void Qjmnet::enableSendMessage() //允许发送消息{ m_inputSendPb->setEnabled(!m_inputLe->text().isEmpty());}void Qjmnet::connectServer(){ // m_connectPb->setEnabled(false); // m_disconnectPb->setEnabled(true); blockSize = 0; tcpSocket->abort(); tcpSocket->connectToHost(getHost(),m_portNum->text().toInt()); m_testLe->setText("has connected");}void Qjmnet::readServerData(){/* char abc[1024]; tcpSocket->read(abc,tcpSocket->bytesAvailable()); m_output_textEdit->setText(abc);*/////////// ///////////////////////////////////////////////////////////////////// QDataStream in(tcpSocket); in.setVersion(QDataStream::Qt_4_1); char abc[512]; tcpSocket->read(abc,tcpSocket->bytesAvailable()); //in >> abc; m_output_textEdit->append(abc);}void Qjmnet::disconnectServer(){ // m_disconnectPb->setEnabled(false); // m_connectPb->setEnabled(true); m_testLe->setText("has disconnected"); tcpSocket->disconnectFromHost();}void Qjmnet::hasConnected(){ m_testLe->setText("has connected"); ///////////////////////发送数据 QByteArray block; QDataStream out(&block,QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_1); /* QMessageBox::about(this, tr("test"), tr("<font color=#ff0000><strong>has run to here</strong></font><br> (c)2004-2007 tony usst. <br>Author: tony tang <tony.tanglin@gmail.com>"));*/ QString text=m_inputLe->text(); QByteArray msg = text.toUtf8(); // QByteArray data = "MESSAGE " + QByteArray::number(msg.size()) + " " + msg; tcpSocket->write(msg);}void Qjmnet::hasDisconnected(){ //qDebug()<<"Disconnected!"; m_testLe->setText("has disconnected"); m_output_textEdit->setText("byebye tony!disconnected!");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -