Save values to a config file
-
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
And the config.ini file[clicks] config=5
-
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 -
@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.
-
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
And the config.ini file[clicks] config=5
@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.
-
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? -
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?@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.
-
Ok, here's the output:
I've also added:qDebug() << "Settings status: " << settings.status();
Which is the "Settings status: ".
@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.
-
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.
-
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.
-
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.
-
Where do you want to store your configuration ?
-