Qt实现企业信息管理系统(1)

[0]首先实现登陆界面:

#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H

#include <QDialog>
#include "QString"
#include "QPoint"

namespace Ui {
class LoginDialog;
class Modify_Dialog;
}

class LoginDialog : public QDialog
{
    Q_OBJECT

public:
    explicit LoginDialog(QWidget *parent = 0);
    ~LoginDialog();
    void do_send_name(){emit send_name(account,rank);}//留给外部接口,为了隐藏account
signals:
    void send_name(const QString &name,const int rank );//信号函数,传递用户名
private slots:
    void on_loginbutton_clicked();
    void on_Exitbutton_clicked();
    void on_loginbutton_2_clicked();

protected:
    void mousePressEvent(QMouseEvent *event); //鼠标按下事件
    void mouseMoveEvent(QMouseEvent *event);//鼠标移动事件
private:
    bool test(QString,QString);
    bool eventFilter(QObject* obj,QEvent* event);
    void do_set_text(QObject* selected,QObject* unselected,QString s);
    void save_password(QString new_password);
private:
    Ui::LoginDialog *ui;
    QString account;
    QString password;
    int rank;
    QPoint windowPos;
    QPoint mousePos;
    QPoint dPos;
};

#endif // LOGINDIALOG_H
#include "logindialog.h"
#include "QFile"
#include "QDebug"
#include "ui_logindialog.h"
#include "ui_modify_dlg.h"
#include "connection.h"
#include "QDir"
#include "QMovie"
#include "QMouseEvent"
#include "QSqlError"
LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowFlags(Qt::FramelessWindowHint);
    auto movie = new QMovie();
    ui->movie_label->setMovie(movie);
    movie->setFileName(":/Image/mygif");
    //movie->setScaledSize(QSize(650,200));
    movie->start();
   ui->Mainsplitter->setStretchFactor(0,2);
   ui->Mainsplitter->setStretchFactor(1,2);
   ui->Mainsplitter->setStretchFactor(2,3);
   ui->accountid->installEventFilter(this);
   ui->password->installEventFilter(this);
   ui->accountid->setPlaceholderText("account id");
   ui->password->setPlaceholderText("password");
    Connection c;
    if(!c.connect("test"))
    {
        qDebug()<<"connect database failed!";
        return;
    }
    ui->IsRemebered->setChecked(true);
    ui->password->setEchoMode(QLineEdit::Password);
    ui->warning->setVisible(false);//设置警告信息为不可见
    this->setWindowTitle("Login Dialog");


    QFile f ("./resource_file/data.txt");//打开纪录帐号的文件
    if(f.size()!=0)//存在帐号记录
    {
        if(f.open(QFile::ReadOnly|QFile::Text))
        {
            QTextStream in(&f);
            in>>account>>password;
            ui->accountid->setText(account);
            ui->password->setText(password);
        }
    }
}

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

void LoginDialog::mousePressEvent(QMouseEvent *event)
{


     windowPos = this->pos();         // 获得部件当前位置
     mousePos = event->globalPos();     // 获得鼠标位置
     dPos = mousePos - windowPos;       // 计算鼠标位置和空间位置的差值
}
void LoginDialog::mouseMoveEvent(QMouseEvent *event)
{
     this->move(event->globalPos() - this->dPos);

}


void LoginDialog::on_loginbutton_clicked()
{

    if(test(ui->accountid->text(),ui->password->text()))//登陆成功
    {
         QFile f ("./resource_file/data.txt");//打开纪录帐号的文件
          if(!f.open(QFile::WriteOnly|QFile::Text))//清空文件且以txt格式打开
          {
              qDebug()<<"File open failed";
               return;
          }
        if(Qt::Checked == ui->IsRemebered->checkState())
        {

                QTextStream out(&f);
                out<<account<<"\n"<<password;//写入文件
         }
          f.close();//关闭文件
        accept();//传出登陆成功的信号
 }
    else
     ui->warning->setVisible(true);
}

bool LoginDialog::test(QString name, QString key)//检测帐号密码的合理性
{

    QSqlQuery query;//创建一个用来查询account数据库的连接.
    account = name;
    password = key;

   query.exec(tr("select password_value,rank from account where account_id = '%1'").arg(account));
   if(query.next()&&query.value(0).toString()==key)
   {
       rank = query.value(1).toInt();
      return true;
   }

    return false;
}

void LoginDialog::on_Exitbutton_clicked()
{
    close();
}

void LoginDialog::do_set_text(QObject *selected, QObject *unselected,QString s)
{
    auto _selected = static_cast<QLineEdit*>(selected);
    auto _unselected = static_cast<QLineEdit*>(unselected);
   _selected->setPlaceholderText("");
   _unselected->setPlaceholderText(s);


}

/******************************事件过滤器**********************************************/
bool LoginDialog::eventFilter(QObject* obj,QEvent* event)
{

    if(obj == ui->accountid||obj==ui->password)//如果事件是从account传过来
    {
        if(event->type()==QEvent::MouseButtonPress)//如果这个事件是鼠标按下
        {
            //auto MouseEvent = static_cast<QMouseEvent*>(event);//强制转换,event是没有key()函数的
            if(obj == ui->accountid)
                do_set_text(obj,ui->password,"input password");
            else
                do_set_text(obj,ui->accountid,"input account id");
               return true;
         }
    }
 return QDialog::eventFilter(obj,event);//对于其他组件,不确定是否有过滤器
}


void LoginDialog::on_loginbutton_2_clicked()
{
    Ui::Modify_Dialog ui;
    auto dlg = new QDialog(this);
   ui.setupUi(dlg);
   dlg->show();

   QObject::connect(ui.save_button,QPushButton::clicked,this,[=](){
       auto _account = ui.account_edit->text();
       auto _old_password = ui.old_password->text();
       auto _new_password = ui.new_password->text();
        if(test(_account,_old_password))
            save_password(_new_password);
        else
        {
            QMessageBox msgBox(QMessageBox::Information, "Error","Wrong account or password");
            msgBox.exec();
        }
       });
}
void LoginDialog::save_password(QString new_password)
{

    qDebug()<<"start";
    QSqlQuery query;//创建一个用来查询account数据库的连接.
   if(query.exec(tr("update account set password_value = '%1' where account_id = '%12' ").
              arg(new_password).arg(account)))//设置数据库的数据改变
   {
    QMessageBox msgBox(QMessageBox::Information, "Ok","Save successfully");
   msgBox.exec();
   }
   else
       qDebug()<<query.lastError().text();
}

需要注意的有以下几点,是在写代码时碰到的问题.

(1):对于资源文件,直接使用WriteOnly模式打开是不行的.具体原因我也不知道,这个地方弄了2个多小时才解决.我们将要写的文件放在当前应用程序运行的目录(使用QDir::CurrentPath()获取)下面,然后使用相对路径创(…\)建一个QFile.

(2):对于信号函数,只能声明不能定义!!!!这个地方又弄了半小时….好像是mooc会将信号部分的声明自动实现其定义.

(3):利用信号将用户名和其他信息传递给我们生成的主对话框类.

(4)实现管理布局,分别实现局部的布局和定级的布局.

(5)设置好样式表,注意不要让父窗口的背景影响到部件,应该用#父窗口名{}的格式来设置样式表.

(6)使用placehodertext可以设置在没有文字的时候显示提示符号.(灰色的文字)

(7)为了美化界面,将对话框的边框隐藏,且设置为透明.但是这样不能移动登陆界面了,需要自己写事件和对应的处理函数来移动.

(8)加载了动态图片作为背景显示.注意用分裂器设置比例.

(9)实现修改密码的功能.

(10)所有的帐号密码都存储在数据库里面.

Qt实现企业信息管理系统(1)_第1张图片

你可能感兴趣的:(Qt实现企业信息管理系统(1))