forked from lyq/smart-greenhouse
Compare commits
1 Commits
master
...
NetworkUti
| Author | SHA1 | Date | |
|---|---|---|---|
| ec527ded18 |
@ -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
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!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>
|
||||
<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.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="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="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"></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>
|
||||
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
|
||||
</valuemap>
|
||||
|
||||
89
cnetworkutil.cpp
Normal file
89
cnetworkutil.cpp
Normal 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
41
cnetworkutil.h
Normal 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
|
||||
20
main.cpp
20
main.cpp
@ -1,11 +1,31 @@
|
||||
#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();
|
||||
}
|
||||
|
||||
34
mainmenu.cpp
34
mainmenu.cpp
@ -1,6 +1,6 @@
|
||||
#include "mainmenu.h"
|
||||
#include "ui_mainmenu.h"
|
||||
|
||||
#include <QMetaMethod>
|
||||
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();
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
#include <QTime>
|
||||
|
||||
#include "submenu.h"
|
||||
#include "cnetworkutil.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui { class MainMenu; }
|
||||
|
||||
271
mainmenu.ui
271
mainmenu.ui
@ -17,131 +17,152 @@
|
||||
<property name="focusPolicy">
|
||||
<enum>Qt::WheelFocus</enum>
|
||||
</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>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 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>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
<property name="geometry">
|
||||
|
||||
Loading…
Reference in New Issue
Block a user