From 466761915f5b068d36dc50dee4d04bf94b255661 Mon Sep 17 00:00:00 2001 From: touwoyimuli Date: Sat, 23 Nov 2019 00:03:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20TcpClient=20=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E5=86=99=E6=A7=BD=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QtTcpEx/ExTcpClient/ExTcpClient.cpp | 37 +++++++++++++++++ QtTcpEx/ExTcpClient/ExTcpClient.h | 17 ++++++++ QtTcpEx/ExTcpClient/ExTcpClient.pro | 11 +++-- QtTcpEx/ExTcpClient/ExTcpClient.ui | 64 ++++++++++++++++++++++++++--- QtTcpEx/ExTcpClient/resource.qrc | 8 ++++ 5 files changed, 126 insertions(+), 11 deletions(-) create mode 100644 QtTcpEx/ExTcpClient/resource.qrc diff --git a/QtTcpEx/ExTcpClient/ExTcpClient.cpp b/QtTcpEx/ExTcpClient/ExTcpClient.cpp index 2240a6f..d67638d 100644 --- a/QtTcpEx/ExTcpClient/ExTcpClient.cpp +++ b/QtTcpEx/ExTcpClient/ExTcpClient.cpp @@ -12,3 +12,40 @@ ExTcpClient::~ExTcpClient() { delete ui; } + +QString ExTcpClient::getLocalIp() +{ + QString hostName = QHostInfo::localHostName(); + QHostInfo hostInfo = QHostInfo::fromName(hostName); + ui->plainTextEdit->appendPlainText("本机名称:" + hostName); + QString localIp; + + foreach (QHostAddress addr, hostInfo.addresses()) { + if (QAbstractSocket::IPv4Protocol == addr.toString()) { + localIp = addr.toString(); + break; + } + } + + return localIp; +} + +void ExTcpClient::on_actConnect_triggered() +{ + +} + +void ExTcpClient::on_actDisconnect_triggered() +{ + +} + +void ExTcpClient::on_actClear_triggered() +{ + ui->plainTextEdit->clear(); +} + +void ExTcpClient::on_actQuit_triggered() +{ + close(); +} diff --git a/QtTcpEx/ExTcpClient/ExTcpClient.h b/QtTcpEx/ExTcpClient/ExTcpClient.h index 309353a..e242fb1 100644 --- a/QtTcpEx/ExTcpClient/ExTcpClient.h +++ b/QtTcpEx/ExTcpClient/ExTcpClient.h @@ -2,6 +2,9 @@ #define EXTCPCLIENT_H #include +#include +#include +#include namespace Ui { class ExTcpClient; @@ -15,8 +18,22 @@ public: explicit ExTcpClient(QWidget *parent = nullptr); ~ExTcpClient(); +private: + QString getLocalIp(); //获取本本机 IP + + +private slots: + //UI 定义的槽函数 + void on_actConnect_triggered(); //请求连接到服务器 + void on_actDisconnect_triggered(); //断开与服务器的连接 + void on_actClear_triggered(); //清除内容 + void on_actQuit_triggered(); //退出程序 + private: Ui::ExTcpClient *ui; + + QLabel* m_labSocket; + QTcpSocket* m_tcpSocket; }; #endif // EXTCPCLIENT_H diff --git a/QtTcpEx/ExTcpClient/ExTcpClient.pro b/QtTcpEx/ExTcpClient/ExTcpClient.pro index 5225567..4681561 100644 --- a/QtTcpEx/ExTcpClient/ExTcpClient.pro +++ b/QtTcpEx/ExTcpClient/ExTcpClient.pro @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core gui +QT += core gui network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -51,8 +51,7 @@ qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target -DISTFILES += \ - images/Image2.jpg \ - images/Image1.png \ - images/Image4.png \ - images/Image5.png +DISTFILES += + +RESOURCES += \ + resource.qrc diff --git a/QtTcpEx/ExTcpClient/ExTcpClient.ui b/QtTcpEx/ExTcpClient/ExTcpClient.ui index ad7579c..9fd8c82 100644 --- a/QtTcpEx/ExTcpClient/ExTcpClient.ui +++ b/QtTcpEx/ExTcpClient/ExTcpClient.ui @@ -6,8 +6,8 @@ 0 0 - 400 - 300 + 561 + 353 @@ -81,7 +81,7 @@ - + 发送 @@ -96,7 +96,7 @@ 0 0 - 400 + 561 25 @@ -108,9 +108,63 @@ false + + + + + + + + :/images/Image4.png:/images/Image4.png + + + 请求连接 + + + 请求连接到服务器 + + + + + + :/images/Image5.png:/images/Image5.png + + + 断开连接 + + + 断开与服务器的连接 + + + + + + :/images/Image1.png:/images/Image1.png + + + 清空 + + + 清空编辑框文本 + + + + + + :/images/Image2.jpg:/images/Image2.jpg + + + 退出 + + + 退出程序 + + - + + + diff --git a/QtTcpEx/ExTcpClient/resource.qrc b/QtTcpEx/ExTcpClient/resource.qrc new file mode 100644 index 0000000..4697e3f --- /dev/null +++ b/QtTcpEx/ExTcpClient/resource.qrc @@ -0,0 +1,8 @@ + + + images/Image5.png + images/Image4.png + images/Image2.jpg + images/Image1.png + +