基于Qt的用户注册和用户管理界面(包含数据库链接)

这个属于是学习中的产物了,在编写的过程中思路改了好几次(主要是管理界面)

一开始用的qt的ui界面设计,后来发现真不如直接写代码布局(也算是学到了)

头文件

//mainwidget.h
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include<QPushButton>
#include"Mold.h"

#include<QSqlQuery>
#include<QLabel>


QT_BEGIN_NAMESPACE
namespace Ui { class MainWidget; }
QT_END_NAMESPACE

class MainWidget : public QWidget
{
    Q_OBJECT
public:
    Mold Reg;
public:
    MainWidget(QWidget *parent = nullptr);
    ~MainWidget();
private:
    QLabel *label_user;
    QLabel *label_password;
    QPushButton *SyStem_Entry;
private slots:
    void on_System_Exit_clicked();
    void on_System_Entry_clicked();

private:
    Ui::MainWidget *ui;
};
#endif // MAINWIDGET_H

//Mold.h
#ifndef REGISTER_WIDGET_H
#define REGISTER_WIDGET_H
#include<QWidget>
#include<QPushButton>
#include<QRadioButton>
#include<QTextEdit>
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QLabel>
class QSuperPushBotton:public QPushButton{
    Q_OBJECT
public:
    QPushButton *Del_PushButton;
    QString PrimaryKey;
private:

private slots:
    void Del_sql();
};

class Mold : public QWidget{
    Q_OBJECT

public:
    Mold(QWidget *parent = nullptr);
    QString Order_control;
    int DataMode;
    void Row_repaint();
    void updata();
    void Table_Show();
    void set_datarow();
    ~Mold();
private slots:
    void set_mode();
    void Mode_E();
public:
    QVBoxLayout *_window;
    QLabel *user;
    QLabel *password;
    QLabel *control;
    //总体布局
        //分体布局:标题
        QHBoxLayout *Layout_Title;
        //分体布局:输入和修改
        QHBoxLayout *Button_Layout;
        QHBoxLayout *TextEdit_Layout;
        //分体布局:列表
        QVBoxLayout *VLayout_user;
        QVBoxLayout *VLayout_password;
        QVBoxLayout *VLayout_control;
        QVBoxLayout *VLayout_button;
        QHBoxLayout *HLayout_information;
private:
        //分体布局:输入和修改
    QLabel *Lable_Warn;
    QPushButton *PushButton_confirm;
    QPushButton *Pushbutton_exit;
    QRadioButton *RadioButton_add;
    QRadioButton *RadioButton_delete;
    QTextEdit *TextEdit_user;
    QTextEdit *TextEdit_password;
    QTextEdit *TextEdit_control;
    QTextEdit *TextEdit;
       //分体布局列表
    QLabel *Lable_user;
    QLabel *Lable_password;
    QLabel *Lable_control;
       //定义的一个继承QPushButton的可以传递其他数据的类
    QSuperPushBotton *Del;

    QString *S_user;
};

#endif // REGISTER_WIDGET_H
//SqlDatabase.h
#ifndef REGISTER_WIDGET_H
#define REGISTER_WIDGET_H
#include<QWidget>
#include<QPushButton>
#include<QRadioButton>
#include<QTextEdit>
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QLabel>
class QSuperPushBotton:public QPushButton{
    Q_OBJECT
public:
    QPushButton *Del_PushButton;
    QString PrimaryKey;
private:

private slots:
    void Del_sql();
};

class Mold : public QWidget{
    Q_OBJECT

public:
    Mold(QWidget *parent = nullptr);
    QString Order_control;
    int DataMode;
    void Row_repaint();
    void updata();
    void Table_Show();
    void set_datarow();
    ~Mold();
private slots:
    void set_mode();
    void Mode_E();
public:
    QVBoxLayout *_window;
    QLabel *user;
    QLabel *password;
    QLabel *control;
    //总体布局
        //分体布局:标题
        QHBoxLayout *Layout_Title;
        //分体布局:输入和修改
        QHBoxLayout *Button_Layout;
        QHBoxLayout *TextEdit_Layout;
        //分体布局:列表
        QVBoxLayout *VLayout_user;
        QVBoxLayout *VLayout_password;
        QVBoxLayout *VLayout_control;
        QVBoxLayout *VLayout_button;
        QHBoxLayout *HLayout_information;
private:
        //分体布局:输入和修改
    QLabel *Lable_Warn;
    QPushButton *PushButton_confirm;
    QPushButton *Pushbutton_exit;
    QRadioButton *RadioButton_add;
    QRadioButton *RadioButton_delete;
    QTextEdit *TextEdit_user;
    QTextEdit *TextEdit_password;
    QTextEdit *TextEdit_control;
    QTextEdit *TextEdit;
       //分体布局列表
    QLabel *Lable_user;
    QLabel *Lable_password;
    QLabel *Lable_control;
       //定义的一个继承QPushButton的可以传递其他数据的类
    QSuperPushBotton *Del;

    QString *S_user;
};

#endif // REGISTER_WIDGET_H

 cpp

//main.cpp
#include "mainwidget.h"
#include "Mold.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWidget w;
    w.show();
    return a.exec();
}
#include "mainwidget.h"
#include "ui_mainwidget.h"
#include"Mold.h"
#include<QDebug>
#include<QSqlError>
#include<QLabel>
#include<regex>
#include"SqlDatabase.h"
//数据库创建类
#include<QSqlDatabase>
//字符串处理类


MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    //链接数据库
    ui->System_Entry->setText("注册");
    ui->System_Exit->setText("管理界面");
    //禁止窗口最大化
    setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
    //固定窗口大小,以UI设计为准
    setFixedSize(width(),height());
    //隐藏用户和密码的提示
    ui->label_user->hide();
    ui->label_password->hide();
    //设置输入密码的控件为密文
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    //权限列表
    ui->comboBox_Perm->addItem("管理员",1);
    ui->comboBox_Perm->addItem("普通用户",2);
    //限制用户名和密码长度
    ui->lineEdit_user->setMaxLength(16);
    ui->lineEdit_password->setMaxLength(16);
}

MainWidget::~MainWidget()
{
    delete ui;
    //关闭数据库
}

void MainWidget::on_System_Exit_clicked()
{

    Reg.show();
    Reg.set_datarow();
}

void MainWidget::on_System_Entry_clicked(){
    QSqlDatabase Save = sqlDatabase::Getdatabase();
    Save.open();
    //再次初始化用户名和密码提示的状态
    ui->label_user->hide();
    ui->label_password->hide();
    //获得输入
    QString user = ui->lineEdit_user->text();
    qDebug() << user << "\n";
    QString password = ui->lineEdit_password->text();
    //设置对错信号
    bool flag = 1;
    //创建调色板
    QPalette PointColor = ui->label_user->palette();
    PointColor.setColor(QPalette::WindowText,Qt::red);
    ui->label_user->setPalette(PointColor);
    ui->label_password->setPalette(PointColor);
    //提示错误
    if(user.length()<5||user.length()>15){
        ui->label_user->setText("用户名过长或过短");
        ui->label_user->show();
        flag = 0;
    }
    if(password.length()<5||password.length()>15){
        PointColor = ui->label_password->palette();
        ui->label_password->setText("密码过长或过短");
        ui->label_password->show();
        flag = 0;
    }
    QSqlQuery Login = QSqlQuery(Save);
    //获得权限的代号
    int control = ui->comboBox_Perm->currentIndex();
    //操作语句传递中介
    QString sql_control;
    //若对则录入数据库
    if(flag==1){
    sql_control = "insert into user(users,passwords,control) Values('"+user+"','"+password+"',"+char(control+48)+")";
    Login.prepare(sql_control);
    Login.exec();
    }
    Reg.set_datarow();
    //清空输入框信息
    ui->lineEdit_user->clear();
    ui->lineEdit_password->clear();
}

列表的显示我是专门写了个重新setLayout的函数来刷新页面,每次刷新之前都要,可能效率会有些低

然后每次重新显示的时候我都用一个指针去遍历这个数据库的数据然后把按钮的地址与数据绑定(通过写了一个继承QPushButton的QSuperPushButton来绑定数据和地址以实现点击删除按钮就能删除)

//register_Widgt.cpp
#include"Mold.h"
#include"mainwidget.h"
#include <QWidget>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QTextEdit>
#include<QDebug>
#include<QLabel>
#include<QSize>
#include"SqlDatabase.h"

QSqlDatabase Save = sqlDatabase::Getdatabase();
QSqlQuery con = QSqlQuery(Save);

Mold *pass;

Mold::Mold(QWidget *parent)
    : QWidget(parent)
{
      update();
      pass = this;
      _window = new QVBoxLayout();
      //创建数据库操作
      QSqlQuery Csql = QSqlQuery(Save);
      Save.open();
      //创建标题
      Layout_Title = new QHBoxLayout();
      QLabel *Lable_Title = new QLabel("用户注册信息");
      QFont set = Lable_Title->font();
      set.setPixelSize(30);
      set.setBold(1);
      QSize size(100,30);
      Lable_Title->resize(size);
      Lable_Title->setFont(set);
      Layout_Title->addWidget(Lable_Title);
      //按钮事件创建并装入窗体
      Button_Layout = new QHBoxLayout();
      RadioButton_add = new QRadioButton("添加");
      RadioButton_delete =new QRadioButton("删除");
      Lable_Warn = new QLabel("");
      Lable_Warn->setFont(set);
      RadioButton_add->setFont(set);
      RadioButton_delete->setFont(set);
      Button_Layout->addWidget(RadioButton_add);
      Button_Layout->addWidget(RadioButton_delete);
      Button_Layout->addWidget(Lable_Warn);
      connect(RadioButton_add,SIGNAL(clicked(bool)),this,SLOT(set_mode()));
      connect(RadioButton_delete,SIGNAL(clicked(bool)),this,SLOT(set_mode()));
      //创建输入框
      TextEdit_Layout = new QHBoxLayout();
      TextEdit_user = new QTextEdit();
      TextEdit_password = new QTextEdit();
      TextEdit_control = new QTextEdit();
      Lable_user = new QLabel("用户名");
      Lable_password = new QLabel("密码");
      Lable_control = new QLabel("权限");
      PushButton_confirm = new QPushButton("确定");
      connect(PushButton_confirm,SIGNAL(clicked(bool)),this,SLOT(Mode_E()));
      //设置字体
      TextEdit_user->setFont(set);
      TextEdit_password->setFont(set);
      TextEdit_control->setFont(set);
      Lable_user->setFont(set);
      Lable_password->setFont(set);
      Lable_control->setFont(set);
      //设置高度
      TextEdit_user->setFixedHeight(40);
      TextEdit_password->setFixedHeight(40);
      TextEdit_control->setFixedHeight(40);
      //创建索引列表(垂直布局)
      TextEdit_Layout->addWidget(Lable_user);
      TextEdit_Layout->addWidget(TextEdit_user);
      TextEdit_Layout->addWidget(Lable_password);
      TextEdit_Layout->addWidget(TextEdit_password);
      TextEdit_Layout->addWidget(Lable_control);
      TextEdit_Layout->addWidget(TextEdit_control);
      TextEdit_Layout->addWidget(PushButton_confirm);
      HLayout_information = new QHBoxLayout();
      //user,password,control
      VLayout_user = new QVBoxLayout();
      VLayout_password = new QVBoxLayout();
      VLayout_control = new QVBoxLayout();
      VLayout_button = new QVBoxLayout();
      //创建索引列表(横向布局)
      HLayout_information->addLayout(VLayout_user);
      HLayout_information->addStretch();
      HLayout_information->addLayout(VLayout_password);
      HLayout_information->addStretch();
      HLayout_information->addLayout(VLayout_control);
      HLayout_information->addStretch();
      HLayout_information->addLayout(VLayout_button);

      //测试类文本
      QTextEdit *TextEdit_test = new QTextEdit("这是一条测试类文本");
      //添加窗口到主窗口(_window)
      _window->addLayout(Layout_Title);
      _window->addLayout(Button_Layout);
      _window->addLayout(TextEdit_Layout);
      _window->addLayout(HLayout_information);
      _window->addWidget(TextEdit_test);
      //显示
      setLayout(_window);
}
Mold::~Mold(){

}
//删除模块
void QSuperPushBotton::Del_sql(){
    QSqlQuery del = QSqlQuery(Save);
    QString qOrder;
    del.exec("select * from user");
    do{
        if(del.value("users")==this->PrimaryKey){
            setLayout(pass->VLayout_user);
            QLayoutItem *item1 = pass->VLayout_user->takeAt(del.at());
            item1->widget()->deleteLater();

            QLayoutItem *item2 = pass->VLayout_password->takeAt(del.at());
            item2->widget()->deleteLater();

            QLayoutItem *item3 = pass->VLayout_control->takeAt(del.at());
            item3->widget()->deleteLater();

            QLayoutItem *item4 = pass->VLayout_button->takeAt(del.at());
            item4->widget()->deleteLater();
            del.exec("delete from user where users = '"+this->PrimaryKey +"'");
        }

    }while(del.next());
    pass->setLayout(pass->_window);
}

void Mold::updata(){

}

void Mold::set_datarow(){
    QSqlQuery *Csql = &con;
    Csql->exec("select * from user");

    QLayoutItem *item =nullptr;
    while((item = VLayout_user->takeAt(0))!=nullptr){

        item->widget()->setParent(NULL);
        delete item->widget();
        delete item;
    }
    while((item = VLayout_password->takeAt(0))!=nullptr){

        item->widget()->setParent(NULL);
        delete item->widget();
        delete item;
    }
    while((item = VLayout_control->takeAt(0))!=nullptr){
        item->widget()->setParent(NULL);
        delete item->widget();
        delete item;
    }
    while((item = VLayout_button->takeAt(0))!=nullptr){
        item->widget()->setParent(NULL);
        delete item->widget();
        delete item;
    }

    while(Csql->next()){
            QFont Row_Set;
            Row_Set.setPixelSize(30);
            user = new QLabel();
            user->setText(Csql->value("users").toString());
            user->setFont(Row_Set);
            VLayout_user->addWidget(user);
            password = new QLabel();
            password->setText(Csql->value("passwords").toString());
            password->setFont(Row_Set);
            VLayout_password->addWidget(password);
            control = new QLabel();
            QString control_name;
            if(Csql->value("control")=="0")
                control_name = "管理员";
            else if(Csql->value("control")=="1")
                control_name = "普通用户";
            control->setText(control_name);
            control->setFont(Row_Set);
            VLayout_control->addWidget(control);
            Del = new QSuperPushBotton();
            Del->Del_PushButton = new QPushButton("删除");
            Del->PrimaryKey=Csql->value("users").toString();
            VLayout_button->addWidget(Del->Del_PushButton);
            connect(Del->Del_PushButton,SIGNAL(clicked(bool)),Del,SLOT(Del_sql()));
    }
}
//一控改,二控删
void Mold::set_mode(){
    if(RadioButton_add->isChecked()){
        DataMode = 1;
    }
    if(RadioButton_delete->isChecked()){
        DataMode = 2;
    }
}
void Mold::Mode_E(){
    qDebug() << DataMode;
    QSqlQuery *change = &con;
    QString S_user = TextEdit_user->toPlainText();
    change->exec("select * from user");
    bool flag=0;
    while(change->next())
        if(S_user == change->value("users"))
            flag =1;

    if(flag == 1)
    change->exec("update user set passwords = '" + TextEdit_password->toPlainText() +"' where users = '" + S_user +"'");
    else
        Lable_Warn->setText("用户 "+S_user+" 不存在");

    set_datarow();
 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值