Compare commits

...

1 Commits

Author SHA1 Message Date
ec527ded18 NetworkUtil接管 2022-01-20 17:29:47 +08:00
8 changed files with 318 additions and 150 deletions

View File

@ -15,12 +15,14 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \ SOURCES += \
cnetworkutil.cpp \
main.cpp \ main.cpp \
mainmenu.cpp \ mainmenu.cpp \
sql.cpp \ sql.cpp \
submenu.cpp submenu.cpp
HEADERS += \ HEADERS += \
cnetworkutil.h \
mainmenu.h \ mainmenu.h \
sql.h \ sql.h \
submenu.h submenu.h

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2022-01-20T14:36:46. --> <!-- Written by QtCreator 4.11.1, 2022-01-20T16:57:37. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
@ -287,19 +287,19 @@
</valuelist> </valuelist>
<value type="int" key="PE.EnvironmentAspect.Base">2</value> <value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/> <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.CustomExecutableRunConfiguration.Executable"></value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:C:/Users/sn04058/Desktop/qt/smart-greenhouse/SmartGreenhouse.pro</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value> <value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">C:/Users/sn04058/Desktop/qt/smart-greenhouse/SmartGreenhouse.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="QString" key="RunConfiguration.Arguments"></value> <value type="QString" key="RunConfiguration.Arguments"></value>
<value type="bool" key="RunConfiguration.Arguments.multi">false</value> <value type="bool" key="RunConfiguration.Arguments.multi">false</value>
<value type="QString" key="RunConfiguration.OverrideDebuggerStartup"></value> <value type="QString" key="RunConfiguration.OverrideDebuggerStartup"></value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value> <value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value> <value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseMultiProcess">false</value> <value type="bool" key="RunConfiguration.UseMultiProcess">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value> <value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value> <value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value> <value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default"></value> <value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/sn04058/Desktop/qt/build-SmartGreenhouse-Desktop_Qt_5_14_2_MinGW_64_bit-Debug</value>
</valuemap> </valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value> <value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap> </valuemap>

89
cnetworkutil.cpp Normal file
View File

@ -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()));
}

41
cnetworkutil.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef CNETWORKUTIL_H
#define CNETWORKUTIL_H
#include <QMainWindow>
#include <QObject>
#include <QWidget>
#include <QTcpSocket> //
#include <QHostAddress> //地址格式
#include <QDebug>
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

View File

@ -1,11 +1,31 @@
#include "mainmenu.h" #include "mainmenu.h"
#include "cnetworkutil.h"
#include <QApplication> #include <QApplication>
void showConnected()
{
if(networkUtil->isConnected())
{
qDebug("connected");
}
else
{
qDebug("unconnected");
}
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
MainMenu w; MainMenu w;
showConnected();
//networkUtil->connect("192.168.72.129",8888);
showConnected();
w.show(); w.show();
return a.exec(); return a.exec();
} }

View File

@ -1,6 +1,6 @@
#include "mainmenu.h" #include "mainmenu.h"
#include "ui_mainmenu.h" #include "ui_mainmenu.h"
#include <QMetaMethod>
MainMenu::MainMenu(QWidget *parent) MainMenu::MainMenu(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainMenu) , ui(new Ui::MainMenu)
@ -30,12 +30,14 @@ void MainMenu::on_pushButton_clicked()
{ {
tcpSocket = new QTcpSocket; tcpSocket = new QTcpSocket;
QString ip = ui->lineEdit_3->text(); 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) void MainMenu::on_checkBox_stateChanged(int arg1)
@ -56,24 +58,21 @@ void MainMenu::on_checkBox_stateChanged(int arg1)
} }
void MainMenu::show_connected() void MainMenu::show_connected()
{ {
int ret = QMessageBox::information(this, tr("👨‍👩‍👦‍👦 登录成功"),tr("点击取消退出连接"), int ret = QMessageBox::information(this, tr("👨‍👩‍👦‍👦 登录成功"),
tr("点击取消退出连接"),
QMessageBox::Ok, QMessageBox::Ok,
QMessageBox::Cancel); QMessageBox::Cancel);
if(ret == QMessageBox::Cancel) if(ret == QMessageBox::Cancel)
{ {
// tcpSocket->disconnected(); networkUtil->disconnect();
delete tcpSocket;
return; return;
} }
else { else {
//跳转到子界面 //跳转到子界面
submenu *SubWindow = new submenu; submenu *SubWindow = new submenu;
connect(SubWindow,SIGNAL(widget_close()),this,SLOT(subWindowClosed() )); connect(SubWindow,SIGNAL(widget_close()),this,SLOT(subWindowClosed()));
SubWindow->show(); SubWindow->show();
this->hide(); this->hide();
// this->close();
} }
} }
@ -87,12 +86,6 @@ void MainMenu::show_disconnected()
} }
if(ret == QMessageBox::No) if(ret == QMessageBox::No)
{ {
// 退出处理?
// if(tcpSocket)
// {
// delete tcpSocket;
// }
// Sleep(20);
return; return;
} }
else if(ret == QMessageBox::Yes) else if(ret == QMessageBox::Yes)
@ -113,5 +106,6 @@ void MainMenu::Sleep(int msec)
void MainMenu::subWindowClosed() void MainMenu::subWindowClosed()
{ {
show(); show();
tcpSocket->close(); networkUtil->disconnect();
//tcpSocket->close();
} }

View File

@ -10,6 +10,7 @@
#include <QTime> #include <QTime>
#include "submenu.h" #include "submenu.h"
#include "cnetworkutil.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainMenu; } namespace Ui { class MainMenu; }

View File

@ -17,131 +17,152 @@
<property name="focusPolicy"> <property name="focusPolicy">
<enum>Qt::WheelFocus</enum> <enum>Qt::WheelFocus</enum>
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <widget class="QWidget" name="">
<item row="0" column="1"> <property name="geometry">
<spacer name="verticalSpacer"> <rect>
<property name="orientation"> <x>9</x>
<enum>Qt::Vertical</enum> <y>9</y>
</property> <width>793</width>
<property name="sizeHint" stdset="0"> <height>533</height>
<size> </rect>
<width>20</width> </property>
<height>204</height> <layout class="QGridLayout" name="gridLayout">
</size> <item row="0" column="1">
</property> <spacer name="verticalSpacer">
</spacer> <property name="orientation">
</item> <enum>Qt::Vertical</enum>
<item row="1" column="0"> </property>
<spacer name="horizontalSpacer_2"> <property name="sizeHint" stdset="0">
<property name="orientation"> <size>
<enum>Qt::Horizontal</enum> <width>17</width>
</property> <height>201</height>
<property name="sizeHint" stdset="0"> </size>
<size> </property>
<width>254</width> </spacer>
<height>20</height> </item>
</size> <item row="1" column="0">
</property> <spacer name="horizontalSpacer_2">
</spacer> <property name="orientation">
</item> <enum>Qt::Horizontal</enum>
<item row="1" column="1"> </property>
<layout class="QVBoxLayout" name="verticalLayout_3"> <property name="sizeHint" stdset="0">
<item> <size>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <width>251</width>
<item> <height>17</height>
<layout class="QVBoxLayout" name="verticalLayout_2"> </size>
<item> </property>
<widget class="QLabel" name="label"> </spacer>
<property name="text"> </item>
<string>账户:</string> <item row="1" column="1" rowspan="2" colspan="2">
</property> <layout class="QVBoxLayout" name="verticalLayout_3">
</widget> <item>
</item> <layout class="QHBoxLayout" name="horizontalLayout_3">
<item> <item>
<widget class="QLabel" name="label_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<property name="text"> <item>
<string>密码:</string> <widget class="QLabel" name="label">
</property> <property name="text">
</widget> <string>账户:</string>
</item> </property>
<item> </widget>
<widget class="QLabel" name="label_3"> </item>
<property name="text"> <item>
<string>IP:</string> <widget class="QLabel" name="label_2">
</property> <property name="text">
</widget> <string>密码:</string>
</item> </property>
</layout> </widget>
</item> </item>
<item> <item>
<layout class="QVBoxLayout" name="verticalLayout"> <widget class="QLabel" name="label_3">
<item> <property name="text">
<widget class="QLineEdit" name="lineEdit_2"/> <string>IP:</string>
</item> </property>
<item> </widget>
<widget class="QLineEdit" name="lineEdit"/> </item>
</item> </layout>
<item> </item>
<widget class="QLineEdit" name="lineEdit_3"> <item>
<property name="text"> <layout class="QVBoxLayout" name="verticalLayout">
<string>172.16.3.135</string> <item>
</property> <widget class="QLineEdit" name="lineEdit_2"/>
</widget> </item>
</item> <item>
</layout> <widget class="QLineEdit" name="lineEdit"/>
</item> </item>
</layout> <item>
</item> <layout class="QHBoxLayout" name="horizontalLayout_2">
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <widget class="QLineEdit" name="lineEdit_3">
<item> <property name="text">
<widget class="QPushButton" name="pushButton"> <string>127.0.0.1</string>
<property name="text"> </property>
<string>登录</string> </widget>
</property> </item>
</widget> <item>
</item> <widget class="QLineEdit" name="lePort">
<item> <property name="text">
<widget class="QCheckBox" name="checkBox"> <string>8888</string>
<property name="layoutDirection"> </property>
<enum>Qt::RightToLeft</enum> </widget>
</property> </item>
<property name="text"> </layout>
<string>显示密码</string> </item>
</property> </layout>
</widget> </item>
</item> </layout>
</layout> </item>
</item> <item>
</layout> <layout class="QHBoxLayout" name="horizontalLayout">
</item> <item>
<item row="1" column="2"> <widget class="QPushButton" name="pushButton">
<spacer name="horizontalSpacer"> <property name="text">
<property name="orientation"> <string>登录</string>
<enum>Qt::Horizontal</enum> </property>
</property> </widget>
<property name="sizeHint" stdset="0"> </item>
<size> <item>
<width>254</width> <widget class="QCheckBox" name="checkBox">
<height>20</height> <property name="layoutDirection">
</size> <enum>Qt::RightToLeft</enum>
</property> </property>
</spacer> <property name="text">
</item> <string>显示密码</string>
<item row="2" column="1"> </property>
<spacer name="verticalSpacer_2"> </widget>
<property name="orientation"> </item>
<enum>Qt::Vertical</enum> </layout>
</property> </item>
<property name="sizeHint" stdset="0"> </layout>
<size> </item>
<width>20</width> <item row="2" column="3">
<height>204</height> <spacer name="horizontalSpacer">
</size> <property name="orientation">
</property> <enum>Qt::Horizontal</enum>
</spacer> </property>
</item> <property name="sizeHint" stdset="0">
</layout> <size>
<width>254</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="2">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>204</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget> </widget>
<widget class="QMenuBar" name="menubar"> <widget class="QMenuBar" name="menubar">
<property name="geometry"> <property name="geometry">