Qt:一鍵導出客戶端(一)-- 登錄窗口

緒論

此篇文章包括登錄窗口主要代碼,可能會涉及到其它模塊代碼的引入。

源碼

ExportData.pro

#-------------------------------------------------
#
# Project created by QtCreator 2019-06-24T15:09:16
#
#-------------------------------------------------

QT       += sql
QT       += core gui
QT       += axcontainer

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = ExportData
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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

CONFIG += c++11

SOURCES += \
        main.cpp \
    mainwindow.cpp \
    config.cpp \
    login.cpp \
    userwindow.cpp \
    mydatabase.cpp \
    myexcel.cpp \
    mytxt.cpp

HEADERS += \
    mainwindow.h \
    config.h \
    login.h \
    userwindow.h \
    mydatabase.h \
    myexcel.h \
    mytxt.h

FORMS += \
        login.ui \
    mainwindow.ui \
    userwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

main.cpp

#include "login.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Login w;
    w.show();

    return a.exec();
}

login.h

#ifndef LOGIN_H
#define LOGIN_H

#include "mainwindow.h"
#include "userwindow.h"

#include <QDialog>

namespace Ui {
class Login;
}

class Login : public QDialog
{
    Q_OBJECT

public:
    explicit Login(QWidget *parent = nullptr);
    ~Login();

private slots:
    // 登錄
    void on_loginBtn_clicked();

private:
    Ui::Login *ui;
    MainWindow *mainWindow;
    UserWindow *userWindow;
};

#endif // LOGIN_H

login.cpp

#include "login.h"
#include "ui_login.h"
#include "config.h"

#include <QMessageBox>
#include <QDateTime>

Login::Login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    this->setWindowTitle("賬號登錄");
}

Login::~Login()
{
    delete ui;
}

void Login::on_loginBtn_clicked()
{
    if (ui->username->text().trimmed() == "") {
        QMessageBox::warning(this, tr("警告!"), tr("賬號不能爲空!"), QMessageBox::Ok);
        return;
    }
    if (ui->password->text().trimmed() == "") {
        QMessageBox::warning(this, tr("警告!"), tr("密碼不能爲空!"), QMessageBox::Ok);
        return;
    }

    // 分流
    // 管理員界面
    if (ui->username->text().trimmed() == tr("admin")) {
        if (ui->password->text().trimmed() == tr("admin%1234")) {
            // 獲取上次登錄時間
            QDateTime loginTime = Config().Get("loginTime", "admin").toDateTime();
            if (loginTime.isNull()) {
                loginTime.currentDateTime();
            }
            Config().Set("loginTime", "admin", QDateTime::currentDateTime());
            Config().Set("loginTime", "lsTime", loginTime);

            this->close();
            mainWindow = new MainWindow;
            mainWindow->setWindowTitle("歡迎您:管理員");
            mainWindow->show();
        }
        else {
            QMessageBox::warning(this, tr("警告!"), tr("密碼錯誤!"), QMessageBox::Ok);
            ui->password->clear();
            ui->password->setFocus();
        }
    }
    // 用戶界面
    else {
        // 獲取用戶信息
        QString userName = Config().Get("user", "userName").toString();
        QString userPass = Config().Get("user", "userPass").toString();

        if (ui->username->text().trimmed() == userName
                && ui->password->text().trimmed() == userPass) {
            // 獲取上次登錄時間
            QDateTime loginTime = Config().Get("loginTime", "user").toDateTime();
            if (loginTime.isNull()) {
                loginTime.currentDateTime();
            }
            Config().Set("loginTime", "user", QDateTime::currentDateTime());
            Config().Set("loginTime", "lsTime", loginTime);

            this->close();
            userWindow = new UserWindow;
            userWindow->setWindowTitle("歡迎您:" + userName);
            userWindow->show();
        }
        else {
            QMessageBox::warning(this, tr("警告!"), tr("賬號或密碼錯誤!"), QMessageBox::Ok);
            ui->password->clear();
            ui->username->setFocus();
        }
    }

}

login.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Login</class>
 <widget class="QDialog" name="Login">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>249</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Login</string>
  </property>
  <widget class="QPushButton" name="loginBtn">
   <property name="geometry">
    <rect>
     <x>131</x>
     <y>150</y>
     <width>141</width>
     <height>31</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>14</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>登錄</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_1">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>40</y>
     <width>72</width>
     <height>31</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>14</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>賬號:</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>90</y>
     <width>72</width>
     <height>31</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>14</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>密碼:</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="username">
   <property name="geometry">
    <rect>
     <x>170</x>
     <y>40</y>
     <width>181</width>
     <height>31</height>
    </rect>
   </property>
  </widget>
  <widget class="QLineEdit" name="password">
   <property name="geometry">
    <rect>
     <x>170</x>
     <y>90</y>
     <width>181</width>
     <height>31</height>
    </rect>
   </property>
   <property name="echoMode">
    <enum>QLineEdit::Password</enum>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

效果

在這裏插入圖片描述

(若有什麼錯誤,請留言指正,3Q)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章