Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Saving the total number of users

Saving the total number of users

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 437 Views
  • 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 Offline
    T Offline
    TheDomesticUser
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      2
      • T Offline
        T Offline
        TheDomesticUser
        wrote on last edited by
        #3

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

        mrjjM 1 Reply Last reply
        0
        • T TheDomesticUser

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @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
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved