Java教程

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();
}

这篇关于Qt学生成绩管理系统登录按钮连接到数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!