1. 程式人生 > >QT 上位機(網路通訊)

QT 上位機(網路通訊)

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))); // connect(this->tcpSocket,SIGNAL(connected()),this,SLOT(haveConnected())); // connect(this->tcpSocket,SIGNAL(disconnected()),this,SLOT(disConnect())); // connect(ui->connectButton,SIGNAL(clicked(bool)),this,SLOT(to_disConnect())); } Client::~Client() { delete ui; } void Client::newConnect() { //QMessageBox::about(this,"提示","連線成功"); tcpSocket->connectToHost(ui->hostLineEdit->text(),ui->portLineEdit->text().toInt()); ui->connectButton->setText("斷開"); } void Client::disConnect() { //QMessageBox::about(this,"提示","斷開連線"); 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(QString(thread.timer.timetoString)); 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(); //thread.start(); if_button_down = false; } else//斷開 { disConnect(); //thread.stop(); if_button_down = true; } } void Client::on_clearButton_clicked() { ui->receiveText->clear(); } void Client::on_receiveText_textChanged() { //ui->receiveText->moveCursor(QTextCursor::End); } 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類中可呼叫顯示時間)

/////////////////////////////////////////////////////////////////////////////////////////
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include "ui_client.h"
#include <QThread>
#include "mytimer.h"

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;
};

#endif // MYTHREAD_H
///////////////////////////////////////////////////////////////////////


#include "mythread.h"
#include "ui_client.h"
#include <QDebug>

#include <QTimer>
#include <qdatetime.h>

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;
};



//namespace Ui {
//class MyThread;
//}


//class MyThread : public QThread
//{
//    Q_OBJECT
//public:
//    explicit MyThread(QObject *parent = 0);
//    void stop();
//    QString gettime();
//protected:
//    void run();
//private:
//    volatile bool stopped;
//};

#endif // MYTIMER_H
///////////////////////////////////////////////////////////////////////////////////
MyTimer::MyTimer()
{

}

void MyTimer::gettimer()
{
    QDateTime time = QDateTime::currentDateTime();
    timetoString = time.toString("yyyy-MM-dd hh:mm:ss");//yyyy-MM-dd hh:mm:ss dddd
    qDebug() << QString(timetoString);
}

UI介面個名稱看程式自己設定 ,原始碼請訪問