smart-greenhouse/mainmenu.cpp
2022-01-20 18:06:11 +08:00

122 lines
2.9 KiB
C++

#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(":/OIP-C3.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 {
//跳转到子界面
SubWindow = new submenu;
connect(SubWindow,SIGNAL(widget_close()),this,SLOT(subWindowClosed() ));
SubWindow->show();
this->hide();
// this->close();
}
}
void MainMenu::show_disconnected()
{
if(SubWindow!=NULL)
{
SubWindow->close();
}
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();
}