Qt是一个跨平台的 C++ 开发库,主要用来开发图形用户界面程序,当然也可以开发不带界面的命令行程序,并且qt开发在很多企业中都会使用,那qt安装需要账号吗?下面来我们就来给大家讲解一下。
安装Qt需要用到账号,如果没有账号需要申请一下。
qt如何实现账号登陆功能?
1、login.h
# ifndef LOGIN_H# define LOGIN_H#include <QWidget> namespace Ui { class Login; } class Login: public QWidget { Q_OBJECT public: explicit Login(QWidget * parent = 0); ~Login(); private slots: void on_btn_login_clicked(); void on_btn_register_clicked(); private: Ui::Login * ui; };# endif // WIDGET_H
2、login.cpp
#include "login.h" #include "ui_login.h" #include "register.h" #include "mainwindow.h" #include <QMessageBox> #include <QSqlQuery> #include <QFile> #include <QDebug> Login::Login(QWidget * parent): QWidget(parent) , ui(new Ui::Login) { ui - > setupUi(this); ui - > ledit_password - > setEchoMode(QLineEdit::Password); } Login::~Login() { delete ui; } void Login::on_btn_login_clicked() { QString username = ui - > ledit_username - > text(); QString password = ui - > ledit_password - > text(); if (username == "" || password == "") { QMessageBox::information(this, "警告", "输入不能为空", QMessageBox::Ok); } else { QSqlQuery query; query.prepare("select username,password from admin where username=:username and password = :password "); query.bindValue(":username", username); query.bindValue(":password", password); query.exec(); if (!query.next()) { //结果集为空 //执行某操作 QMessageBox::information(this, "警告", "用户名或密码错误!", QMessageBox::Ok); } else { QMessageBox::information(this, "提醒", "登录成功!", QMessageBox::Ok); MainWindow * m = new MainWindow; m - > show(); this - > close(); } } } void Login::on_btn_register_clicked() { Register * r = new Register; r - > show(); }
3、register.h
# ifndef REGISTER_H# define REGISTER_H#include <QWidget> namespace Ui { class Register; } class Register: public QWidget { Q_OBJECT public: explicit Register(QWidget * parent = 0); ~Register(); private slots: void on_btn_logon_clicked(); private: Ui::Register * ui; };# endif // REGISTER_H
4、register.cpp
#include "register.h" #include "ui_register.h" #include <QButtonGroup> #include <QMessageBox> #include <QRegExp> #include <QSqlQuery> Register::Register(QWidget * parent): QWidget(parent) , ui(new Ui::Register) { ui - > setupUi(this); } Register::~Register() { delete ui; } void Register::on_btn_logon_clicked() { QString username = ui - > ledit_username - > text(); QString password = ui - > ledit_pwd - > text(); QString name = ui - > ledit_name - > text(); int age = ui - > ledit_age - > text() .toInt(); QButtonGroup * bg = new QButtonGroup(this); bg - > addButton(ui - > rbtn_male, 0); //一个值为0 bg - > addButton(ui - > rbtn_female, 1); //一个值为1 int sel = bg - > checkedId(); //取到你所选的radioButton的值 QString gender; switch (sel) { case 0: gender = "男"; break; case 1: gender = "女"; break; default: gender = ""; break; } QSqlQuery query; query.prepare("select username from patient where username=:username"); query.bindValue(":username", username); query.exec(); if (query.next()) { QMessageBox::information(this, "警告", "用户名已存在!", QMessageBox::Ok); } else { query.prepare("insert into patient(username,password,patientName,age,gender)" "values(:username,:password,:patientName,:age,:gender)"); query.bindValue(":username", username); query.bindValue(":password", password); query.bindValue(":patientName", name); query.bindValue(":age", age); query.bindValue(":gender", gender); query.exec(); QMessageBox::information(this, "警告", "注册成功!", QMessageBox::Ok); } }
5、数据库连接代码
# ifndef CONNECTION# define CONNECTION#include <QSqlDatabase> #include <QStringList> #include <QString> #include <QDebug> #include <QSqlQuery> #include <QMessageBox> static bool createConnection() { //测试用例:连接mysql数据库,做一个基本的sql语句操作 //1、对qt下数据库的驱动进行遍历查看 QStringList drivers = QSqlDatabase::drivers(); foreach(QString driver, drivers) { qDebug() << drivers; } //2、打开数据库过程 QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); //数据库连接的信息进行配置 db.setHostName("localhost"); //设置主机名(数据库所在电脑的名称) db.setDatabaseName("medical_system"); //设置数据库名称 db.setUserName("root"); db.setPassword("123456"); //db.setPort(3306);//因为是本机,该段代码可省略 if (!db.open()) { //打开失败的情况 qDebug() << "Failed to connect"; //实际情况下我们应该使用图形化窗口提示打开失败 QMessageBox::critical(0, "无法打开数据库", "无法创建", QMessageBox::Yes); return false; } return true; }# endif // CONNECTION
运行结果:
Qt下载安装肯定是需要账号的,如果大家没有账号,可以去qt官网注册一个之后再进行qt安装哦!最后大家如果想要了解更多json相关知识,敬请关注奇Q工具网。
推荐阅读: