ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2022-01-20 17:00:14  阅读:159  来源: 互联网

标签:logindialog LoginDialog Qt 管理系统 deprecated db 按钮 include 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有