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. Save values to a config file
Forum Updated to NodeBB v4.3 + New Features

Save values to a config file

Scheduled Pinned Locked Moved Solved General and Desktop
43 Posts 7 Posters 7.0k Views 4 Watching
  • 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.
  • S Offline
    S Offline
    Sucharek
    wrote on 7 Sept 2021, 14:20 last edited by
    #1

    Hi, I want to save some variable values into a config file.
    I tried doing it with QSettings and resources, but it doesn't seem to work.

    My mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QSettings>
    #include <QDebug>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
    
        void on_horizontalSlider_valueChanged(int value);
    
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    
    

    My mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    int test;
    int someValue;
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        QSettings settings(QString(":/config/config.ini"), QSettings::IniFormat);
        settings.setValue("clicks/config", test);
        someValue = settings.value("clicks/config", "").toInt();
        qDebug() << someValue;
    }
    
    void MainWindow::on_horizontalSlider_valueChanged(int value)
    {
        test = value;
    }
    
    

    The resources
    8be1dd2a-4731-4364-ad35-b80377201d7f-image.png
    And the config.ini file

    [clicks]
    config=5
    
    J 1 Reply Last reply 7 Sept 2021, 16:02
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 7 Sept 2021, 14:40 last edited by
      #2

      QSettings provides no way of reading INI "path" entries, i.e., entries with unescaped slash characters. (This is because these entries are ambiguous and cannot be resolved automatically.)
      https://doc.qt.io/qt-5/qsettings.html#QSettings-3

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mchinand
        wrote on 7 Sept 2021, 15:26 last edited by mchinand 9 Jul 2021, 15:34
        #3

        @Sucharek said in Save values to a config file:

        QSettings settings(QString(":/config/config.ini"), QSettings::IniFormat);

        I don't think you can store the settings in a resource file (":/config..."). Changing a setting would mean the executable would need to be modified during execution.

        1 Reply Last reply
        6
        • S Sucharek
          7 Sept 2021, 14:20

          Hi, I want to save some variable values into a config file.
          I tried doing it with QSettings and resources, but it doesn't seem to work.

          My mainwindow.h

          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          #include <QSettings>
          #include <QDebug>
          
          QT_BEGIN_NAMESPACE
          namespace Ui { class MainWindow; }
          QT_END_NAMESPACE
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              MainWindow(QWidget *parent = nullptr);
              ~MainWindow();
          
          private slots:
              void on_pushButton_clicked();
          
              void on_horizontalSlider_valueChanged(int value);
          
          private:
              Ui::MainWindow *ui;
          };
          #endif // MAINWINDOW_H
          
          

          My mainwindow.cpp

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              , ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          }
          
          int test;
          int someValue;
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          void MainWindow::on_pushButton_clicked()
          {
              QSettings settings(QString(":/config/config.ini"), QSettings::IniFormat);
              settings.setValue("clicks/config", test);
              someValue = settings.value("clicks/config", "").toInt();
              qDebug() << someValue;
          }
          
          void MainWindow::on_horizontalSlider_valueChanged(int value)
          {
              test = value;
          }
          
          

          The resources
          8be1dd2a-4731-4364-ad35-b80377201d7f-image.png
          And the config.ini file

          [clicks]
          config=5
          
          J Online
          J Online
          JonB
          wrote on 7 Sept 2021, 16:02 last edited by JonB 9 Jul 2021, 16:04
          #4

          @Sucharek said in Save values to a config file:

          QSettings settings(QString(":/config/config.ini"), QSettings::IniFormat);

          As @mchinand has said. Start by checking settings.isWritable() after this line

          One reason why isWritable() might return false is if QSettings operates on a read-only file.

          You might also check QSettings::status() for an error.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sucharek
            wrote on 7 Sept 2021, 18:04 last edited by
            #5

            Hi, sorry for the late response. I was away for about 3 hours, but I'm back now.
            I've tried the settings.isWritable() but that didn't work.
            I also didn't understand the QSettings::status(). Could you give me an example please?

            M J 2 Replies Last reply 7 Sept 2021, 18:09
            0
            • S Sucharek
              7 Sept 2021, 18:04

              Hi, sorry for the late response. I was away for about 3 hours, but I'm back now.
              I've tried the settings.isWritable() but that didn't work.
              I also didn't understand the QSettings::status(). Could you give me an example please?

              M Offline
              M Offline
              mchinand
              wrote on 7 Sept 2021, 18:09 last edited by
              #6

              What was the value returned by settings.isWritable()?

              1 Reply Last reply
              0
              • S Sucharek
                7 Sept 2021, 18:04

                Hi, sorry for the late response. I was away for about 3 hours, but I'm back now.
                I've tried the settings.isWritable() but that didn't work.
                I also didn't understand the QSettings::status(). Could you give me an example please?

                J Online
                J Online
                JonB
                wrote on 7 Sept 2021, 18:14 last edited by JonB 9 Jul 2021, 18:15
                #7

                @Sucharek said in Save values to a config file:

                I've tried the settings.isWritable() but that didn't work.

                It's not supposed to "make it work"! It's supposed to tell you why it likely isn't working.

                We're trying to tell you that you cannot save settings to a resource file.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sucharek
                  wrote on 7 Sept 2021, 18:15 last edited by
                  #8

                  It didn't output anything

                  J M 2 Replies Last reply 7 Sept 2021, 18:16
                  0
                  • S Sucharek
                    7 Sept 2021, 18:15

                    It didn't output anything

                    J Online
                    J Online
                    JonB
                    wrote on 7 Sept 2021, 18:16 last edited by
                    #9

                    @Sucharek said in Save values to a config file:

                    It didn't output anything

                    Please use the documentation to understand.

                    1 Reply Last reply
                    0
                    • S Sucharek
                      7 Sept 2021, 18:15

                      It didn't output anything

                      M Offline
                      M Offline
                      mchinand
                      wrote on 7 Sept 2021, 18:17 last edited by
                      #10

                      @Sucharek

                      What is the output of
                      qDebug() << "Is Settings Writeable? " << settings.isWritable();

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Sucharek
                        wrote on 7 Sept 2021, 18:25 last edited by
                        #11

                        Ok, here's the output:
                        f7d2302d-f00d-4ab6-acbb-7770c403c48f-image.png
                        I've also added:

                        qDebug() << "Settings status: " << settings.status();
                        

                        Which is the "Settings status: ".

                        J 1 Reply Last reply 7 Sept 2021, 18:31
                        0
                        • S Sucharek
                          7 Sept 2021, 18:25

                          Ok, here's the output:
                          f7d2302d-f00d-4ab6-acbb-7770c403c48f-image.png
                          I've also added:

                          qDebug() << "Settings status: " << settings.status();
                          

                          Which is the "Settings status: ".

                          J Online
                          J Online
                          JonB
                          wrote on 7 Sept 2021, 18:31 last edited by JonB 9 Jul 2021, 18:32
                          #12

                          @Sucharek
                          So it isn't writeable. Which we already know, because resource files are not writeable. You cannot set settings in a resource file. That's the answer.

                          Hi, I want to save some variable values into a config file.

                          I tried doing it with QSettings and resources, but it doesn't seem to work.

                          Yes, it does not work.

                          1 Reply Last reply
                          3
                          • S Offline
                            S Offline
                            Sucharek
                            wrote on 7 Sept 2021, 18:39 last edited by
                            #13

                            Ok, so , can I use like a full path? (C:/...)

                            M 1 Reply Last reply 7 Sept 2021, 18:43
                            0
                            • S Sucharek
                              7 Sept 2021, 18:39

                              Ok, so , can I use like a full path? (C:/...)

                              M Offline
                              M Offline
                              mchinand
                              wrote on 7 Sept 2021, 18:43 last edited by
                              #14

                              No, resource files don't have a location on disk that you can specify other than how you have above (:/...), they are stored embedded in the executable.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 7 Sept 2021, 18:46 last edited by
                                #15

                                Hi,

                                If you properly configure your QApplication, QSettings will by default store your settings in the appropriate location for the OS your application runs on.

                                If you really want to have a file, then use QStandardPaths to query a suitable folder.

                                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
                                1
                                • S Offline
                                  S Offline
                                  Sucharek
                                  wrote on 7 Sept 2021, 20:08 last edited by
                                  #16

                                  Ok, so how do I store it in the :/config.ini now?

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 7 Sept 2021, 20:24 last edited by
                                    #17

                                    As was already explained several times by my fellows on this thread: you cannot write in the resources.

                                    These are read-only data embedded in your executable.

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

                                    S 1 Reply Last reply 7 Sept 2021, 20:30
                                    2
                                    • S SGaist
                                      7 Sept 2021, 20:24

                                      As was already explained several times by my fellows on this thread: you cannot write in the resources.

                                      These are read-only data embedded in your executable.

                                      S Offline
                                      S Offline
                                      Sucharek
                                      wrote on 7 Sept 2021, 20:30 last edited by
                                      #18

                                      Oh, ok so now can I use the QStandardPaths? And if so, how?

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 7 Sept 2021, 20:33 last edited by
                                        #19

                                        Where do you want to store your configuration ?

                                        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
                                        0
                                        • S Offline
                                          S Offline
                                          Sucharek
                                          wrote on 7 Sept 2021, 20:40 last edited by
                                          #20

                                          Well, I'm building the app for an Android device, so I would put it to: /data/data/org.qtproject.example.clicker/
                                          if reading/writing to that folder isn't possible (without root), I would put it to:
                                          /sdcard/Android/data/org.qtproject.example.clicker/

                                          J.HilkJ 1 Reply Last reply 8 Sept 2021, 06:17
                                          0

                                          7/43

                                          7 Sept 2021, 18:14

                                          topic:navigator.unread, 36
                                          • Login

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