Compare commits

..

No commits in common. "NetworkUtilAdded" and "master" have entirely different histories.

8 changed files with 150 additions and 318 deletions

View File

@ -15,14 +15,12 @@ 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

View File

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

View File

@ -1,89 +0,0 @@
#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()));
}

View File

@ -1,41 +0,0 @@
#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,31 +1,11 @@
#include "mainmenu.h"
#include "cnetworkutil.h"
#include <QApplication>
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();
}

View File

@ -1,6 +1,6 @@
#include "mainmenu.h"
#include "ui_mainmenu.h"
#include <QMetaMethod>
MainMenu::MainMenu(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainMenu)
@ -30,14 +30,12 @@ void MainMenu::on_pushButton_clicked()
{
tcpSocket = new QTcpSocket;
QString ip = ui->lineEdit_3->text();
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()));
}
tcpSocket->connectToHost(ip,7777);
connect(tcpSocket, SIGNAL(connected()), this, SLOT(show_connected()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(show_disconnected()));
}
void MainMenu::on_checkBox_stateChanged(int arg1)
@ -58,21 +56,24 @@ 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)
{
networkUtil->disconnect();
// tcpSocket->disconnected();
delete tcpSocket;
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();
}
}
@ -86,6 +87,12 @@ void MainMenu::show_disconnected()
}
if(ret == QMessageBox::No)
{
// 退出处理?
// if(tcpSocket)
// {
// delete tcpSocket;
// }
// Sleep(20);
return;
}
else if(ret == QMessageBox::Yes)
@ -106,6 +113,5 @@ void MainMenu::Sleep(int msec)
void MainMenu::subWindowClosed()
{
show();
networkUtil->disconnect();
//tcpSocket->close();
tcpSocket->close();
}

View File

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

View File

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