Saving the total number of users
-
Hello. Recently, I started creating a hotel project where the user can create accounts, log in, gamble at casinos, etc. I am using QSettings as a save functionality for each account, with a variable containing the total number of accounts created. Does anyone know how to increment the total account variable, while also continuing off after saving even when you reopen it? Thanks for answering.
widget.h file:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QVector> #include <QString> #include <QSettings> class Login; namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = nullptr); ~Widget(); private slots: void on_signUpButton_clicked(); void on_logInButton_clicked(); private: Ui::Widget *ui; int totalUsers = 0; QVector<Login> accounts; void signUp(QString, QString); }; class Login { private: QString username; QString password; public: QString returnUsername() { return username; } QString returnPassword() { return password; } }; #endif // WIDGET_H
widget.cpp file:
#include "widget.h" #include "ui_widget.h" #include "signup.h" #include "login.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); } Widget::~Widget() { delete ui; } void Widget::on_signUpButton_clicked() { SignUp *signUp = new SignUp(this); signUp->exec(); } void Widget::on_logInButton_clicked() { LogIn *logIn = new LogIn(this); logIn->exec(); } void Widget::signUp(QString username, QString password) { QSettings settings("Hotel.Inc", "Account Info"); settings.beginGroup("Prefix"); settings.setValue(QString::number(totalUsers) + 'u', username); settings.setValue(QString::number(totalUsers) + 'p', password); // How to use QSettings to save the number of total users? settings.endGroup(); }
-
Hi,
From your project description, you should rather consider using a database to handle your data storage.
-
Thanks for the reply. What are the Qt libraries for handling databases?
-
Thanks for the reply. What are the Qt libraries for handling databases?
@TheDomesticUser
Hi and welcome to the forums.
To start using Qt and database
https://doc.qt.io/qt-5/database.html
Please note you must add
QT += sql
to your .pro file and run qmake from build menu for it to know the classes.You must know a bit of SQL to use them.
small sample
bool createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(":memory:"); // open db in memeory // use this to have a file db.setDatabaseName("c:\\folder\\test.db"); if (!db.open()) { QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.", QMessageBox::Cancel); return false; } QSqlQuery query; // this creates a table with firstname, lastname and a number qDebug() << "table:" << query.exec("create table person (id int primary key, " "firstname varchar(20), lastname varchar(20), num int )"); // put some data in table query.exec("insert into person (firstname , lastname, num) values('Dennis', 'Young','1')"); query.exec("insert into person values(102, 'Christine', 'Holand','2')"); query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')"); query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')"); query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')"); return true; } ---- to read from it, you can use QSqlQuery QSqlQuery query("select * from person;"); or have a look at the models ( which i think fit your case fine) https://doc.qt.io/qt-5/qsqlquerymodel.html https://doc.qt.io/qt-5/qsqlrelationaltablemodel.html