Important: Please read the Qt Code of Conduct -

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
        explicit Widget(QWidget *parent = nullptr);
    private slots:
        void on_signUpButton_clicked();
        void on_logInButton_clicked();
        Ui::Widget *ui;
        int totalUsers = 0;
        QVector<Login> accounts;
        void signUp(QString, QString);
    class Login
        QString username;
        QString password;
        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) :
        ui(new Ui::Widget)
        delete ui;
    void Widget::on_signUpButton_clicked()
        SignUp *signUp = new SignUp(this);
    void Widget::on_logInButton_clicked()
        LogIn *logIn = new LogIn(this);
    void Widget::signUp(QString username, QString password)
        QSettings settings("Hotel.Inc", "Account Info");
        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?

  • Lifetime Qt Champion


    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?

  • Lifetime Qt Champion

    Hi and welcome to the forums.
    To start using Qt and database
    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 (! {
            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)

Log in to reply