This commit is contained in:
1 2022-01-20 14:34:15 +08:00
commit d2a6974b55
18 changed files with 521 additions and 0 deletions

40
SmartGreenhouse.pro Normal file
View File

@ -0,0 +1,40 @@
QT += core gui network sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainmenu.cpp \
sql.cpp \
submenu.cpp
HEADERS += \
mainmenu.h \
sql.h \
submenu.h
FORMS += \
mainmenu.ui \
submenu.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
DISTFILES +=
RESOURCES += \
img/img.qrc

BIN
img/OIP-C.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

BIN
img/OIP-C1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

BIN
img/OIP-C2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

BIN
img/R-C.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

BIN
img/hide.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 403 B

BIN
img/ico.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

10
img/img.qrc Normal file
View File

@ -0,0 +1,10 @@
<RCC>
<qresource prefix="/">
<file>hide.png</file>
<file>show.png</file>
<file>ico.png</file>
<file>R-C.jpg</file>
<file>OIP-C.jpg</file>
<file>OIP-C1.jpg</file>
</qresource>
</RCC>

BIN
img/show.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 537 B

11
main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "mainmenu.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainMenu w;
w.show();
return a.exec();
}

117
mainmenu.cpp Normal file
View File

@ -0,0 +1,117 @@
#include "mainmenu.h"
#include "ui_mainmenu.h"
MainMenu::MainMenu(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainMenu)
{
ui->setupUi(this);
ui->lineEdit_2->setText("admin");
ui->lineEdit->setText("123456");
ui->lineEdit->setEchoMode(QLineEdit::Password);
this->setWindowIcon(QIcon(":/ico.png"));
this->setWindowTitle("快乐不起来啊");
this->setAutoFillBackground(true);
QPalette palette;
QPixmap pixmap(":/Resources/Penguins.jpg");
palette.setBrush(QPalette::Window, QBrush(pixmap));
this->setPalette(palette);
}
MainMenu::~MainMenu()
{
delete ui;
}
void MainMenu::on_pushButton_clicked()
{
tcpSocket = new QTcpSocket;
QString ip = ui->lineEdit_3->text();
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)
{
if(arg1)
{
ui->lineEdit->setEchoMode(QLineEdit::Normal);
}
else if (!arg1)
{
ui->lineEdit->setEchoMode(QLineEdit::Password);
}
else {
qDebug("err: ui->lineEdit->setEchoMode(QLineEdit::Password);");
return;
}
return ;
}
void MainMenu::show_connected()
{
int ret = QMessageBox::information(this, tr("👨‍👩‍👦‍👦 登录成功"),tr("点击取消退出连接"),
QMessageBox::Ok,
QMessageBox::Cancel);
if(ret == QMessageBox::Cancel)
{
// tcpSocket->disconnected();
delete tcpSocket;
return;
}
else {
//跳转到子界面
submenu *SubWindow = new submenu;
connect(SubWindow,SIGNAL(widget_close()),this,SLOT(subWindowClosed() ));
SubWindow->show();
this->hide();
// this->close();
}
}
void MainMenu::show_disconnected()
{
int ret = QMessageBox::information(this, tr("👨‍👩‍👦‍👦 连接断开"),tr("是否重新连接?"),
QMessageBox::Yes,QMessageBox::No,QMessageBox::Cancel);
if(ret == QMessageBox::Cancel)
{
return;
}
if(ret == QMessageBox::No)
{
// 退出处理?
// if(tcpSocket)
// {
// delete tcpSocket;
// }
// Sleep(20);
return;
}
else if(ret == QMessageBox::Yes)
{
ui->pushButton->click();
}
else{
qDebug("error:---");
return ;
}
}
void MainMenu::Sleep(int msec)
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
void MainMenu::subWindowClosed()
{
show();
tcpSocket->close();
}

38
mainmenu.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef MAINMENU_H
#define MAINMENU_H
#include <QString>
#include <QMainWindow>
#include <QFile>
#include <QTcpSocket> //
#include <QHostAddress> //地址格式
#include <QMessageBox>
#include <QTime>
#include "submenu.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainMenu; }
QT_END_NAMESPACE
class MainMenu : public QMainWindow
{
Q_OBJECT
public:
MainMenu(QWidget *parent = nullptr);
~MainMenu();
private slots:
void on_pushButton_clicked();
void on_checkBox_stateChanged(int arg1);
void show_connected();
void show_disconnected();
void Sleep(int msec);
void subWindowClosed();
private:
QTcpSocket * tcpSocket;
Ui::MainMenu *ui;
};
#endif // MAINMENU_H

108
mainmenu.ui Normal file
View File

@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainMenu</class>
<widget class="QMainWindow" name="MainMenu">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainMenu</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>255</x>
<y>217</y>
<width>183</width>
<height>109</height>
</rect>
</property>
<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="text">
<string>显示密码</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

99
sql.cpp Normal file
View File

@ -0,0 +1,99 @@
#include "sql.h"
sql::sql()
{
//创建数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); //添加驱动
db.setDatabaseName("SmartGreenhouse.db"); //设置库名
bool ok = db.open();
if(!ok){
qDebug("open failed");
return ;
}
else {
qDebug()<<"open success";
}
//通过db创建表
//db.exec("create table usr (name text, passwd text); ");
//通过query创建表
QSqlQuery query;
ok = query.exec("create table if not exists user (id text, name text);");
if(!ok){
qDebug("failed to create user table");
}
else {
// qDebug()<<"create table success";
qDebug()<<"Student table created successfully";
}
ok = query.exec("create table if not exists Data (temp text, hum text,light text,co2 text);");
if(!ok){
qDebug("failed to create Data table");
}
else {
qDebug()<<"Data table created successfully";
}
//插入数据库
/*QString id = ui->lineEdit->text();
QString name = ui->lineEdit_2->text();
QString sql = QString("insert into stu values ('%1','%2')").arg(id).arg(name);
qDebug()<<"sql:"<<sql;
QSqlQuery query;
bool ok;
ok = query.exec(sql);
if(!ok){
qDebug()<<"insert table failed";
}
else {
qDebug()<<"insert table success";
}*/
//删除一条记录 根据学号来删除
/*
QString id = ui->lineEdit->text();
QString sql = QString("delete from stu where id='%1'").arg(id);
qDebug()<<"sql:"<<sql;
QSqlQuery query;
bool ok;
ok = query.exec(sql);
if(!ok){
qDebug()<<"delete table failed";
}
else {
qDebug()<<"delete table success";
}*/
//查询数据库
/* QString sql = QString("select * from stu;");
qDebug()<<"sql:"<<sql;
QSqlQuery query;
bool ok;
ok = query.exec(sql);
if(!ok){
qDebug()<<"select table failed";
}
else {
qDebug()<<"select table success";
}
while(query.next()){
qDebug()<< query.value("name").toString();
}*/
}

15
sql.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef SQL_H
#define SQL_H
#include <QObject>
//#include <QSql>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlDatabase>
class sql
{
public:
sql();
};
#endif // SQL_H

23
submenu.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "submenu.h"
#include "ui_submenu.h"
submenu::submenu(QWidget *parent) :
QWidget(parent),
ui(new Ui::submenu)
{
ui->setupUi(this);
this->setWindowIcon(QIcon(":/ico.png"));
this->setWindowTitle("风中有朵雨做的云");
}
submenu::~submenu()
{
delete ui;
}
void submenu::on_pushButton_clicked()
{
this->hide();
emit widget_close();
this->close();
}

28
submenu.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef SUBMENU_H
#define SUBMENU_H
#include <QWidget>
namespace Ui {
class submenu;
}
class submenu : public QWidget
{
Q_OBJECT
public:
explicit submenu(QWidget *parent = nullptr);
~submenu();
private slots:
void on_pushButton_clicked();
private:
Ui::submenu *ui;
signals:
void widget_close(void);
};
#endif // SUBMENU_H

32
submenu.ui Normal file
View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>submenu</class>
<widget class="QWidget" name="submenu">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>160</x>
<y>240</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>退出</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>