diff --git a/SmartGreenhouse.pro b/SmartGreenhouse.pro index 7d2ee79..a17aa4e 100644 --- a/SmartGreenhouse.pro +++ b/SmartGreenhouse.pro @@ -15,12 +15,14 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ + cnetworkutil.cpp \ main.cpp \ mainmenu.cpp \ sql.cpp \ submenu.cpp HEADERS += \ + cnetworkutil.h \ mainmenu.h \ sql.h \ submenu.h diff --git a/SmartGreenhouse.pro.user b/SmartGreenhouse.pro.user index b052cfe..bb2f0df 100644 --- a/SmartGreenhouse.pro.user +++ b/SmartGreenhouse.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -287,19 +287,19 @@ 2 - - ProjectExplorer.CustomExecutableRunConfiguration - + Qt4ProjectManager.Qt4RunConfiguration:C:/Users/sn04058/Desktop/qt/smart-greenhouse/SmartGreenhouse.pro + C:/Users/sn04058/Desktop/qt/smart-greenhouse/SmartGreenhouse.pro false false true + true false false true - + C:/Users/sn04058/Desktop/qt/build-SmartGreenhouse-Desktop_Qt_5_14_2_MinGW_64_bit-Debug 1 diff --git a/cnetworkutil.cpp b/cnetworkutil.cpp new file mode 100644 index 0000000..4323546 --- /dev/null +++ b/cnetworkutil.cpp @@ -0,0 +1,89 @@ +#include "cnetworkutil.h" + + +/* +* TODO: +* 1、receive buffer +* 2、receive callback(connect signal & slot) +* 3、buffer reader +*/ + + +//这是一个全局变量 +CNetWorkUtil *networkUtil = new CNetWorkUtil; + +//构造函数是空的 +CNetWorkUtil::CNetWorkUtil() +{ + +} + +//以默认参数连接 ip 127.0.0.1 port 8080 +void CNetWorkUtil::connect() +{ + //qDebug(this->m_ip.toUtf8()); + this->m_tcpSocket.connectToHost(this->m_ip,this->m_port); +} + +//连接指定参数网址 +void CNetWorkUtil::connect(QString ip, int port) +{ + this->setIp(ip); + this->setPort(port); + this->connect(); +} + +//设置连接的ip +void CNetWorkUtil::setIp(QString ip) +{ + this->m_ip = ip; +} + +//获取当前连接的ip +QString CNetWorkUtil::getIp() +{ + return this->m_ip; +} + +//设置端口号 +void CNetWorkUtil::setPort(int port) +{ + this->m_port = port; +} + +//获取当前连接的端口号 +int CNetWorkUtil::getPort() +{ + return this->m_port; +} + +//判断当前是否连接 +bool CNetWorkUtil::isConnected() +{ + return this->m_tcpSocket.state() == QAbstractSocket::ConnectingState; +} + +//断开网络 +void CNetWorkUtil::disconnect() +{ + //断开连接断开关联 + this->m_tcpSocket.disconnect(&this->m_tcpSocket, SIGNAL(connected()), nullptr, nullptr); + this->m_tcpSocket.disconnect(&this->m_tcpSocket, SIGNAL(disconnected()), nullptr, nullptr); + + this->m_tcpSocket.disconnectFromHost(); + this->m_tcpSocket.close(); +} + + +//获取tcpSocket对象 +QTcpSocket &CNetWorkUtil::getTcpSocket() +{ + return this->m_tcpSocket; +} + + +//发送消息 +void CNetWorkUtil::sendMsg(QString msg) +{ + this->m_tcpSocket.write(msg.toUtf8(),strlen(msg.toUtf8())); +} diff --git a/cnetworkutil.h b/cnetworkutil.h new file mode 100644 index 0000000..803256d --- /dev/null +++ b/cnetworkutil.h @@ -0,0 +1,41 @@ +#ifndef CNETWORKUTIL_H +#define CNETWORKUTIL_H + +#include +#include +#include + +#include // +#include //地址格式 +#include + + +class CNetWorkUtil +{ + //Q_OBJECT + +private: + QTcpSocket m_tcpSocket; + int m_port = 8080; + QString m_ip = "127.0.0.1"; + + +public: + CNetWorkUtil(); + void connect(); + void connect(QString ip,int port); + void setIp(QString ip); + QString getIp(); + void setPort(int port); + int getPort(); + bool isConnected(); + void disconnect(); + QTcpSocket & getTcpSocket(); + void sendMsg(QString msg); + +}; + + +extern CNetWorkUtil *networkUtil; + +#endif // CNETWORKUTIL_H diff --git a/main.cpp b/main.cpp index 05ace16..f8855a4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,31 @@ #include "mainmenu.h" +#include "cnetworkutil.h" + #include +void showConnected() +{ + if(networkUtil->isConnected()) + { + qDebug("connected"); + } + else + { + qDebug("unconnected"); + } + +} + + int main(int argc, char *argv[]) { QApplication a(argc, argv); MainMenu w; + + showConnected(); + //networkUtil->connect("192.168.72.129",8888); + showConnected(); w.show(); return a.exec(); } diff --git a/mainmenu.cpp b/mainmenu.cpp index f5240f6..437cde3 100644 --- a/mainmenu.cpp +++ b/mainmenu.cpp @@ -1,6 +1,6 @@ #include "mainmenu.h" #include "ui_mainmenu.h" - +#include MainMenu::MainMenu(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainMenu) @@ -30,12 +30,14 @@ void MainMenu::on_pushButton_clicked() { tcpSocket = new QTcpSocket; QString ip = ui->lineEdit_3->text(); - tcpSocket->connectToHost(ip,7777); + int port = ui->lePort->text().toInt(); + networkUtil->connect(ip,port); + if(networkUtil->isConnected()) + { + connect(&networkUtil->getTcpSocket(), SIGNAL(connected()), this, SLOT(show_connected())); + connect(&networkUtil->getTcpSocket(), SIGNAL(disconnected()), this, SLOT(show_disconnected())); + } - - - connect(tcpSocket, SIGNAL(connected()), this, SLOT(show_connected())); - connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(show_disconnected())); } void MainMenu::on_checkBox_stateChanged(int arg1) @@ -56,24 +58,21 @@ void MainMenu::on_checkBox_stateChanged(int arg1) } void MainMenu::show_connected() { - int ret = QMessageBox::information(this, tr("👨‍👩‍👦‍👦 登录成功"),tr("点击取消退出连接"), + int ret = QMessageBox::information(this, tr("👨‍👩‍👦‍👦 登录成功"), + tr("点击取消退出连接"), QMessageBox::Ok, QMessageBox::Cancel); if(ret == QMessageBox::Cancel) { - // tcpSocket->disconnected(); - delete tcpSocket; + networkUtil->disconnect(); return; } else { - - //跳转到子界面 submenu *SubWindow = new submenu; - connect(SubWindow,SIGNAL(widget_close()),this,SLOT(subWindowClosed() )); + connect(SubWindow,SIGNAL(widget_close()),this,SLOT(subWindowClosed())); SubWindow->show(); this->hide(); -// this->close(); } } @@ -87,12 +86,6 @@ void MainMenu::show_disconnected() } if(ret == QMessageBox::No) { - // 退出处理? - // if(tcpSocket) - // { - // delete tcpSocket; - // } - // Sleep(20); return; } else if(ret == QMessageBox::Yes) @@ -113,5 +106,6 @@ void MainMenu::Sleep(int msec) void MainMenu::subWindowClosed() { show(); - tcpSocket->close(); + networkUtil->disconnect(); + //tcpSocket->close(); } diff --git a/mainmenu.h b/mainmenu.h index b53fd7b..adff9cb 100644 --- a/mainmenu.h +++ b/mainmenu.h @@ -10,6 +10,7 @@ #include #include "submenu.h" +#include "cnetworkutil.h" QT_BEGIN_NAMESPACE namespace Ui { class MainMenu; } diff --git a/mainmenu.ui b/mainmenu.ui index a68ff69..26abc9d 100644 --- a/mainmenu.ui +++ b/mainmenu.ui @@ -17,131 +17,152 @@ Qt::WheelFocus - - - - - Qt::Vertical - - - - 20 - 204 - - - - - - - - Qt::Horizontal - - - - 254 - 20 - - - - - - - - - - - - - - 账户: - - - - - - - 密码: - - - - - - - IP: - - - - - - - - - - - - - - - - - 172.16.3.135 - - - - - - - - - - - - - 登录 - - - - - - - Qt::RightToLeft - - - 显示密码 - - - - - - - - - - - Qt::Horizontal - - - - 254 - 20 - - - - - - - - Qt::Vertical - - - - 20 - 204 - - - - - + + + + 9 + 9 + 793 + 533 + + + + + + + Qt::Vertical + + + + 17 + 201 + + + + + + + + Qt::Horizontal + + + + 251 + 17 + + + + + + + + + + + + + + 账户: + + + + + + + 密码: + + + + + + + IP: + + + + + + + + + + + + + + + + + + + 127.0.0.1 + + + + + + + 8888 + + + + + + + + + + + + + + + 登录 + + + + + + + Qt::RightToLeft + + + 显示密码 + + + + + + + + + + + Qt::Horizontal + + + + 254 + 20 + + + + + + + + Qt::Vertical + + + + 20 + 204 + + + + + +