登录窗口头文件
# ifndef LOGINUI_H
# define LOGINUI_H # include <QWidget>
# include <QLineEdit>
# include <QPushButton>
# include <QLabel>
# include <QMessageBox> # include <QSqlDatabase>
# include <QSqlQuery>
# include <QSqlRecord> # include <QKeyEvent> # include "client.h" class LoginUI : public QWidget
{ Q_OBJECTprivate : QLabel* Background_Lab; QLabel* Titile_Lab; QLabel* Username_Lab; QLabel* Password_Lab; QPushButton* Close_Btn; QPushButton* Hide_Btn; QPushButton* Login_Btn; QPushButton* Register_Btn; QLineEdit* Username_Edit; QLineEdit* Password_Edit; QSqlDatabase db; bool DrawLoginUI ( ) ; LoginUI ( ) ; bool construct ( ) ; private slots: void On_CloseBtn_Clicked ( ) ; void On_Loginbtn_Slots ( ) ; void On_Registerbtn_Slots ( ) ; public : static LoginUI* NewInstance ( ) ; void show ( ) ; ~ LoginUI ( ) ; signals: void jump ( ) ; } ;
# endif
登录窗口函数实现
# include "LoginUI.h"
# include <QDebug> bool LoginUI :: DrawLoginUI ( )
{ bool ret = true ; Background_Lab = new QLabel ( this ) ; if ( Background_Lab != nullptr ) { Background_Lab-> resize ( 538 , 160 ) ; Background_Lab-> setStyleSheet ( "background-color:rgb(115,70,240)" ) ; } else { ret = false ; } Titile_Lab = new QLabel ( this ) ; if ( Titile_Lab != nullptr ) { Titile_Lab-> resize ( 90 , 50 ) ; Titile_Lab-> setPixmap ( QPixmap ( ":/QT_Icon/QQ_Icon.png" ) ) ; Titile_Lab-> setScaledContents ( true ) ; } else { ret = false ; } Close_Btn = new QPushButton ( this ) ; if ( Close_Btn != nullptr ) { Close_Btn-> resize ( 20 , 20 ) ; Close_Btn-> setIcon ( QIcon ( ":/QT_Icon/close02.png" ) ) ; Close_Btn-> move ( 508 , 10 ) ; connect ( this -> Close_Btn, & QPushButton:: clicked, this , & LoginUI:: On_CloseBtn_Clicked) ; } else { ret = false ; } Hide_Btn = new QPushButton ( this ) ; if ( Hide_Btn != nullptr ) { Hide_Btn-> resize ( 20 , 20 ) ; Hide_Btn-> setIcon ( QIcon ( ":/QT_Icon/hide02.png" ) ) ; Hide_Btn-> move ( 468 , 10 ) ; connect ( this -> Hide_Btn, & QPushButton:: clicked, this , & LoginUI:: hide) ; } else { ret = false ; } Username_Edit = new QLineEdit ( this ) ; if ( Username_Edit != nullptr ) { Username_Edit-> resize ( 260 , 40 ) ; Username_Edit-> move ( 159 , 173 ) ; Username_Edit-> setMaxLength ( 16 ) ; Username_Edit-> setPlaceholderText ( "QQ账号/手机号/邮箱" ) ; } else { ret = false ; } Password_Edit = new QLineEdit ( this ) ; if ( Password_Edit != nullptr ) { Password_Edit-> resize ( 260 , 40 ) ; Password_Edit-> move ( Username_Edit-> x ( ) , Username_Edit-> y ( ) + 60 ) ; Password_Edit-> setEchoMode ( QLineEdit:: Password) ; Password_Edit-> setMaxLength ( 16 ) ; } else { ret = false ; } Username_Lab = new QLabel ( this ) ; if ( Username_Lab != nullptr ) { Username_Lab-> resize ( 30 , 30 ) ; Username_Lab-> move ( Username_Edit-> x ( ) - 40 , Username_Edit-> y ( ) + 5 ) ; Username_Lab-> setPixmap ( QPixmap ( ":/QT_Icon/windos_icon1.png" ) ) ; Username_Lab-> setScaledContents ( true ) ; } else { ret = false ; } Password_Lab = new QLabel ( this ) ; if ( Password_Lab != nullptr ) { Password_Lab-> resize ( 30 , 30 ) ; Password_Lab-> move ( Password_Edit-> x ( ) - 40 , Password_Edit-> y ( ) + 5 ) ; Password_Lab-> setPixmap ( QPixmap ( ":/QT_Icon/password.png" ) ) ; Password_Lab-> setScaledContents ( true ) ; } else { ret = false ; } Login_Btn = new QPushButton ( "登录" , this ) ; if ( Login_Btn != nullptr ) { Login_Btn-> resize ( 130 , 40 ) ; Login_Btn-> setStyleSheet ( "background-color:rgb(8,189,253);" ) ; Login_Btn-> move ( Password_Lab-> x ( ) , Password_Lab-> y ( ) + 60 ) ; connect ( this -> Login_Btn, & QPushButton:: clicked, this , & LoginUI:: On_Loginbtn_Slots) ; } else { ret = false ; } Register_Btn = new QPushButton ( "注册" , this ) ; if ( Register_Btn != nullptr ) { Register_Btn-> resize ( 130 , 40 ) ; Register_Btn-> setStyleSheet ( "background-color:rgb(8,189,253);" ) ; Register_Btn-> move ( Login_Btn-> x ( ) + 170 , Login_Btn-> y ( ) ) ; connect ( this -> Register_Btn, & QPushButton:: clicked, this , & LoginUI:: On_Registerbtn_Slots) ; } else { ret = false ; } return ret;
} LoginUI :: LoginUI ( ) : QWidget ( nullptr , Qt:: WindowCloseButtonHint)
{
} void LoginUI :: show ( )
{ setWindowFlag ( Qt:: FramelessWindowHint) ; setFixedSize ( 538 , 373 ) ; QWidget :: show ( ) ;
} LoginUI :: ~ LoginUI ( )
{
}
bool LoginUI :: construct ( )
{ bool ret = true ; ret = DrawLoginUI ( ) ; if ( ! db. contains ( "UserMsg.db" ) ) { db = QSqlDatabase :: addDatabase ( "QSQLITE" ) ; db. setDatabaseName ( "UserMsg.db" ) ; } if ( ! db. open ( ) ) { QMessageBox :: information ( this , "提示" , "数据库打开失败" ) ; ret = false ; } QString Create_Table_Sql = "create table if not exists user_msg(" "user_name varchar(16) primary key," "password varchar(16))" ; QSqlQuery querry; if ( ! querry. exec ( Create_Table_Sql) ) { QMessageBox :: information ( this , "提示" , "创建表失败" ) ; ret = false ; } return ret; }
void LoginUI :: On_CloseBtn_Clicked ( )
{ QMessageBox box ( QMessageBox:: Question, "关闭" , "您确定要退出?" , QMessageBox:: Yes | QMessageBox:: No, this ) ; box. setDefaultButton ( QMessageBox:: No) ; int ret = box. exec ( ) ; if ( ret == QMessageBox:: Yes) { this -> close ( ) ; } else if ( ret == QMessageBox:: No) { }
}
void LoginUI :: On_Loginbtn_Slots ( )
{ QString name = Username_Edit-> text ( ) ; QString password = Password_Edit-> text ( ) ; if ( name. isEmpty ( ) ) { QMessageBox :: information ( this , "提示" , "请输入用户名" ) ; return ; } else if ( password. isEmpty ( ) ) { QMessageBox :: information ( this , "提示" , "请输入密码" ) ; return ; } QString sql = QString ( "select * from user_msg where user_name = '%1' and password = '%2'" ) . arg ( name) . arg ( password) ; QSqlQuery querry; querry. exec ( sql) ;
if ( ! querry. next ( ) ) { QMessageBox :: information ( this , "提示" , "账号密码错误" ) ; return ; } else { QMessageBox :: information ( this , "提示" , "登陆成功" ) ; emit jump ( ) ; this -> close ( ) ; } }
void LoginUI :: On_Registerbtn_Slots ( )
{ QString name = Username_Edit-> text ( ) ; QString password = Password_Edit-> text ( ) ; if ( name. isEmpty ( ) || password. isEmpty ( ) ) { QMessageBox :: information ( this , "提示" , "请将信息填写完整" ) ; return ; } QString sql = QString ( "insert into user_msg(user_name, password) " "values('%1', '%2')" ) . arg ( name) . arg ( password) ; QSqlQuery querry; if ( ! querry. exec ( sql) ) { QMessageBox :: information ( this , "提示" , "注册失败" ) ; return ; } else { QMessageBox :: information ( this , "提示" , "注册成功" ) ; }
}
LoginUI * LoginUI :: NewInstance ( )
{ LoginUI* ret = new LoginUI; if ( ! ret-> construct ( ) || ret == nullptr ) { delete ret; ret = nullptr ; } return ret;
}