热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

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

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

login1.pro

#
# Project created by QtCreator 2022-01-06T22:10:15
#
#-------------------------------------------------QT += core gui sqlgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsTARGET = 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.0SOURCES += main.cpp\mainwindow.cpp \logindialog.cppHEADERS += mainwindow.h \logindialog.h

logindialog.h

#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H#include
#include
#include
#include
#include
#include
#include 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=&#39;%1&#39;and mima=&#39;%2&#39; ").arg(zhanghao).arg(mima);QSqlQuery query(db); //数据库函数的传参query.exec(s);if(query.first()){QMessageBox::information(NULL, "登陆成功", "登陆成功!!!", QMessageBox::Yes);accept();}elseQMessageBox::warning(NULL,"Error","登录失败,请重试!!!");usrLineEdit->clear();pwdLineEdit->clear();usrLineEdit->setFocus();}

main.cpp

#include "mainwindow.h"
#include
#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();
}

推荐阅读
author-avatar
18382457909@163.com
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有