Client类
#ifndef CLIENT_H
#define CLIENT_H
#include <QDialog>
#include <QAbstractSocket>
#include "mythread.h"
class QTcpSocket;
namespace Ui {
class Client;
}
class Client : public QDialog
{
Q_OBJECT
public:
explicit Client(QWidget *parent = 0);
~Client();
private:
Ui::Client *ui;
QTcpSocket *tcpSocket;
QString message;
QStringList messageList;
bool if_button_down = 1;
MyThread thread;
private slots:
void newConnect();
void disConnect();
void readMessage();
void displayError(QAbstractSocket::SocketError);
void updata();
void on_connectButton_clicked();
void on_saveButton_clicked();
void on_receiveText_textChanged();
void on_clearButton_clicked();
};
#endif // CLIENT_H
#include "client.h"
#include "ui_client.h"
#include <QtNetwork>
#include <QTimer>
#include <qdatetime.h>
#include<QMessageBox>
Client::Client(QWidget *parent) :
QDialog(parent),
ui(new Ui::Client)
{
ui->setupUi(this);
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, &QTcpSocket::readyRead, this, &Client::readMessage);
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
}
Client::~Client()
{
delete ui;
}
void Client::newConnect()
{
tcpSocket->connectToHost(ui->hostLineEdit->text(),ui->portLineEdit->text().toInt());
ui->connectButton->setText("断开");
}
void Client::disConnect()
{
tcpSocket->abort();
ui->connectButton->setText("连接到服务器");
}
void Client::updata()
{
messageList = message.split(" ");
ui->receiveText->append(message);
qDebug() << QString("message receive");
ui->text1->setText(messageList.at(1));
ui->text2->setText(messageList.at(2));
ui->text3->setText(messageList.at(3));
ui->text4->setText(messageList.at(4));
ui->text5->setText(messageList.at(5));
ui->text6->setText(messageList.at(6));
ui->text7->setText(messageList.at(7));
ui->text8->setText(messageList.at(8));
}
void Client::readMessage()
{
message = tcpSocket->readAll();
updata();
}
void Client::displayError(QAbstractSocket::SocketError)
{
qDebug() << tcpSocket->errorString();
QMessageBox::about(this,"提示",tcpSocket->errorString());
}
void Client::on_connectButton_clicked()
{
if(if_button_down)
{
newConnect();
if_button_down = false;
}
else
{
disConnect();
if_button_down = true;
}
}
void Client::on_clearButton_clicked()
{
ui->receiveText->clear();
}
void Client::on_receiveText_textChanged()
{
}
void Client::on_saveButton_clicked()
{
QFile myfile("./mydat.txt");
QString text = ui->receiveText->toPlainText();
myfile.open(QIODevice::WriteOnly |QIODevice::Text | QIODevice::Append);
myfile.write("\r\n**************我是分隔符**************\r\n");
myfile.write(text.toStdString().c_str());
myfile.close();
QMessageBox::about(this,"保存成功","已保存到的当前路径mydat.txt");
}
MyThread 类(被屏蔽,Client类中可调用显示时间)
/////////////////////////////////////////////////////////////////////////////////////////
namespace Ui {
class MyThread;
}
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0);
void stop();
void timeui(Ui::Client *dis);
MyTimer timer;
protected:
void run();
private:
volatile bool stopped;
};
///////////////////////////////////////////////////////////////////////
MyThread::MyThread(QObject *parent) :
QThread(parent)
{
stopped = false;
}
void MyThread::run()
{
while (!stopped)
{
timer.gettimer();
msleep(1000);
}
stopped = false;
}
void MyThread::timeui(Ui::Client *mainui)
{
mainui->receiveText->setText(QString(timer.timetoString));
}
void MyThread::stop()
{
stopped = true;
}
///////////////////////////////////////////////////////////////////////////////
MyTimer类
#ifndef MYTIMER_H
#define MYTIMER_H
#include <QTimer>
#include <qdatetime.h>
class MyTimer : public QTimer
{
public:
MyTimer();
void gettimer();
QString timetoString;
};
#endif // MYTIMER_H
MyTimer::MyTimer()
{
}
void MyTimer::gettimer()
{
QDateTime time = QDateTime::currentDateTime();
timetoString = time.toString("yyyy-MM-dd hh:mm:ss");
qDebug() << QString(timetoString);
}
UI界面个名称看程序自己设置 ,源代码请访问
https://gitee.com/fly-ghost/codes/u4hrpmfcaz16ntvwiqo3e99
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)