#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #include #include #include "MainDialog.h" #include "mysqlutils.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , m_pMainDialog(nullptr) { ui->setupUi(this); this->setWindowIcon(QIcon(":/images/icon.png")); QPixmap pixmap(":/images/hj-net.png"); pixmap = pixmap.scaled(250, 75, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 按比例缩放 ui->label_logo->setPixmap(pixmap); QString qsLineEditStyle("QLineEdit { min-height: 20px; min-width: 120px; }"); ui->userToken->setStyleSheet(qsLineEditStyle); ui->serverIp->setStyleSheet(qsLineEditStyle); QRegularExpression rx("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$"); QRegularExpressionValidator* ipValidator = new QRegularExpressionValidator(rx, this); ui->serverIp->setValidator(ipValidator); //setIp("127.0.0.1"); setIp("192.168.10.254"); } MainWindow::~MainWindow() { delete ui; if (m_pMainDialog) { delete m_pMainDialog; m_pMainDialog = nullptr; } } void MainWindow::setIp(const QString &ip) { ui->serverIp->setText(ip); } bool MainWindow::testDatabase() { QString svr = ui->serverIp->text(); std::string dbserver = svr.toStdString(); //"tcp://192.168.4.254"; int dbport = 3306; std::string dbuser = "root"; std::string dbpasswd = "Hj57471000"; std::string dbname = "hjems"; bool dbok = MysqlUtils::getInstance()->OpenDatabase(dbserver,dbport,dbuser,dbpasswd,dbname); if (!dbok) { hloge("failed to open database, exit now..."); return false; } hlogi("database connection test successfully!"); return true; } void MainWindow::on_pb_Logon_clicked() { if (testDatabase()) { m_pMainDialog = new MainDialog(); m_pMainDialog->show(); this->close(); } else { QMessageBox::critical(this, tr("Critical Message"),tr("Failed to retrieve device data!")); } } void MainWindow::on_pb_Test_clicked() { if (testDatabase()) QMessageBox::information(this, tr("Successfully"),tr("Connect to device successfully!")); else QMessageBox::critical(this, tr("Critical Message"),tr("Failed to connect to device!")); }