Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Saving the total number of users

    General and Desktop
    3
    4
    258
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      TheDomesticUser last edited by

      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();
      }
      
      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        From your project description, you should rather consider using a database to handle your data storage.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 2
        • T
          TheDomesticUser last edited by

          Thanks for the reply. What are the Qt libraries for handling databases?

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @TheDomesticUser last edited by mrjj

            @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
            
            1 Reply Last reply Reply Quote 3
            • First post
              Last post