数据库
首页 > 数据库> > Qt学生成绩管理系统登录按钮连接到数据库

Qt学生成绩管理系统登录按钮连接到数据库

作者:互联网

实现当点击登录界面的登录按钮时,会连接到本地MySQL数据库,根据相应数据表实现登录。

login1.pro

#
# Project created by QtCreator 2022-01-06T22:10:15
#
#-------------------------------------------------

QT       += core gui sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = login1
TEMPLATE = app

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


SOURCES += main.cpp\
        mainwindow.cpp \
    logindialog.cpp

HEADERS  += mainwindow.h \
    logindialog.h

logindialog.h

#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QSqlDatabase>  
#include <QDebug>        
#include<QStringList>   
#include<QSqlQuery>    


class LoginDialog : public QDialog
{
    Q_OBJECT
public:
    LoginDialog(QWidget *parent = 0);
        ~LoginDialog();
    private:
        QLabel *usrLabel;
        QLabel *pwdLabel;
        QLineEdit *usrLineEdit;
        QLineEdit *pwdLineEdit;
        QPushButton *LoginBtn;
        QPushButton *exitBtn;
    private slots:
        void login();
        void close();
    signals:
        void signal_username(QString);

};

#endif // LOGINDIALOG_H

logindialog.cpp

关键代码(连接部分)

void LoginDialog::login(){
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("scts");    //连接的数据库名字
    db.setUserName("root");
    db.setPassword("qazqaz12");
    if(!db.open())               //通过open连接数据库
        qDebug()<<"连接失败";
    else qDebug()<<"连接成功";

    QString zhanghao = usrLineEdit->text();         //zhanghao和mima是sql中对应表的列名
    QString mima = pwdLineEdit->text();
    QString s=QString("select * from guanli where zhanghao='%1'and mima='%2' ").arg(zhanghao).arg(mima);

    QSqlQuery query(db);       //数据库函数的传参
    query.exec(s);
    if(query.first()){
        QMessageBox::information(NULL, "登陆成功", "登陆成功!!!", QMessageBox::Yes);
        accept();

    }
    else
        QMessageBox::warning(NULL,"Error","登录失败,请重试!!!");
         usrLineEdit->clear();
         pwdLineEdit->clear();
         usrLineEdit->setFocus();


}

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include "logindialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    LoginDialog dlg;
    if(dlg.exec()==QDialog::Accepted){
        w.show();
    }
    return a.exec();
}

标签:logindialog,LoginDialog,Qt,管理系统,deprecated,db,按钮,include,QT
来源: https://blog.csdn.net/qq_44918057/article/details/122604868