Save values to a config file
-
wrote on 7 Sept 2021, 15:26 last edited by mchinand 9 Jul 2021, 15:34
@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
wrote on 7 Sept 2021, 16:02 last edited by JonB 9 Jul 2021, 16:04@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.
-
wrote on 7 Sept 2021, 18:04 last edited by
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?wrote on 7 Sept 2021, 18:09 last edited byWhat was the value returned by
settings.isWritable()
? -
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?wrote on 7 Sept 2021, 18:14 last edited by JonB 9 Jul 2021, 18:15@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.
-
wrote on 7 Sept 2021, 18:15 last edited by
It didn't output anything
-
wrote on 7 Sept 2021, 18:16 last edited by
@Sucharek said in Save values to a config file:
It didn't output anything
Please use the documentation to understand.
-
wrote on 7 Sept 2021, 18:25 last edited by
-
Ok, here's the output:
I've also added:qDebug() << "Settings status: " << settings.status();
Which is the "Settings status: ".
wrote on 7 Sept 2021, 18:31 last edited by JonB 9 Jul 2021, 18:32@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.
-
wrote on 7 Sept 2021, 18:39 last edited by
Ok, so , can I use like a full path? (C:/...)
-
wrote on 7 Sept 2021, 18:43 last edited by
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.
-
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.
-
wrote on 7 Sept 2021, 20:08 last edited by
Ok, so how do I store it in the :/config.ini now?
-
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.
wrote on 7 Sept 2021, 20:30 last edited byOh, ok so now can I use the QStandardPaths? And if so, how?
-
Where do you want to store your configuration ?
-
wrote on 7 Sept 2021, 20:40 last edited by
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/ -
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/I actually have a ini file that I ship in one of my applications with the resource system. On startup I check, if my ini file exists in the relating appdata system.
If not I copy it from the resources.This is made for Windows, but since its Qt, it should work just as well for Android.
Here are the bare bone functions:
//From to create class PreAppTasks: void PreAppTasks::copyIniToDestDir() { QFile *destFile = getDestIniFile(); //QFile *orgFile = getOrgIniFile(); //in your case simply: QFile *orgFile = new File(":/config/config.ini"); if(orgFile){ if(destFile && destFile->exists()){ //Both Files exists: implement your logic, for this case here } else { //DestFile does not exist (propably the first program execution) -> simply copy from install folder //But first, create the folder, if it does not exist QFileInfo info(destFile->fileName()); info.setFile(info.absolutePath()); if(!info.exists()){ QDir d; d.mkpath(info.absoluteFilePath()); } orgFile->copy(destFile->fileName()); } } if(destFile){ destFile->deleteLater(); destFile = nullptr; } if(orgFile){ orgFile->deleteLater(); orgFile = nullptr; } } QFile *PreAppTasks::getDestIniFile() { //in your case: return new QFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QString("/config/config.ini")); } //In main.cpp (AFTER application instance creation!) int main(int argc, char *argv[]) { QGuiApplication a(argc, argv); PreAppTasks::copyIniToDestDir(); .... return a.exec(); }
-
I actually have a ini file that I ship in one of my applications with the resource system. On startup I check, if my ini file exists in the relating appdata system.
If not I copy it from the resources.This is made for Windows, but since its Qt, it should work just as well for Android.
Here are the bare bone functions:
//From to create class PreAppTasks: void PreAppTasks::copyIniToDestDir() { QFile *destFile = getDestIniFile(); //QFile *orgFile = getOrgIniFile(); //in your case simply: QFile *orgFile = new File(":/config/config.ini"); if(orgFile){ if(destFile && destFile->exists()){ //Both Files exists: implement your logic, for this case here } else { //DestFile does not exist (propably the first program execution) -> simply copy from install folder //But first, create the folder, if it does not exist QFileInfo info(destFile->fileName()); info.setFile(info.absolutePath()); if(!info.exists()){ QDir d; d.mkpath(info.absoluteFilePath()); } orgFile->copy(destFile->fileName()); } } if(destFile){ destFile->deleteLater(); destFile = nullptr; } if(orgFile){ orgFile->deleteLater(); orgFile = nullptr; } } QFile *PreAppTasks::getDestIniFile() { //in your case: return new QFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QString("/config/config.ini")); } //In main.cpp (AFTER application instance creation!) int main(int argc, char *argv[]) { QGuiApplication a(argc, argv); PreAppTasks::copyIniToDestDir(); .... return a.exec(); }
wrote on 8 Sept 2021, 06:56 last edited by@J-Hilk Thanks for the reply.
I'm not home right now, so I'll try it later.
Then I'll inform you if it works.
12/43