In this recipe, we will learn how put our knowledge to use and create a functional login screen using Qt and MySQL.
Create your first functional login screen by following these steps:
user
, which looks like this:employeeID
to the ID of an existing employee's data. This way, the user account we created will be linked to the data of one of the employees:mainwindow.ui
. First, place a stacked widget on the canvas and make sure it contains two pages. Then, set up the two pages in the stacked widget like this:mainwindow.h
and add the following headers after the line #include <QMainWindow>
:#include <QMainWindow> #include <QtSql> #include <QSqlDatabase> #include <QSqlQuery> #include <QMessageBox> #include <QDebug>
mainwindow.h
:private:
Ui::MainWindow *ui;
QSqlDatabase db;
mainwindow.cpp
and put this code in the class constructor:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->stackedWidget->setCurrentIndex(0); db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("127.0.0.1"); db.setUserName("yourusername"); db.setPassword("yourpassword"); db.setDatabaseName("databasename"); if (!db.open()) { qDebug() << "Failed to connect to database."; } }
void MainWindow::on_loginButton_clicked() { QString username = ui->username->text(); QString password = ui->password->text(); QSqlQuery query; if (query.exec("SELECT employeeID from user WHERE username = '" + username + "' AND password = '" + password + "'")) { if (query.size() > 0) { while (query.next()) { QString employeeID = query.value(0).toString(); QSqlQuery query2; if (query2.exec("SELECT name, age, gender, married FROM employee WHERE id = " + employeeID)) { while (query2.next()) { QString name = query2.value(0).toString(); QString age = query2.value(1).toString(); int gender = query2.value(2).toInt(); bool married = query2.value(3).toBool(); ui->name->setText(name); ui->age->setText(age); if (gender == 0) ui->gender->setText("Male"); else ui->gender->setText("Female"); if (married) ui->married->setText("Yes"); else ui->married->setText("No"); ui->stackedWidget->setCurrentIndex(1); } } } } else { QMessageBox::warning(this, "Login failed", "Invalid username or password."); } } else { qDebug() << query.lastError().text(); } }
void MainWindow::on_logoutButton_clicked() { ui->stackedWidget->setCurrentIndex(0); }
MainWindow::~MainWindow() { db.close(); delete ui; }
In this example, we select data from the user
table that matches the username and password that we have inserted into the text fields. If nothing is found, it means we have provided an invalid username or password. Otherwise, obtain the employeeID
data from the user account and do another SQL query to look for information from the employee
table that matches the employeeID
variable. Then, display the data accordingly to the UI of our program.
We must set the widget order in the Edit Tab Order mode so that when the program has started, the first widget that gets focused on is the username line edit widget. If the user presses on the TAB button on the keyboard, the focus should switch to the second widget, which is the password line edit. Incorrect widget order will totally ruin the user experience and drive away your potential users.
Do make sure that the
echoMode option of the password line edit is set to
Password
. That setting will hide the actual password inserted into the line edit and replace it with dot symbols for security purposes.
18.226.251.206