Save values to a config file
-
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. -
wrote on 8 Sept 2021, 16:05 last edited by
Hi, so I tried to add it to my code, but I don't know where.
Could you please tell me how to do it, or do I need to create a new C++ Class? -
@J-Hilk said in Save values to a config file:
//In main.cpp (AFTER application instance creation!)
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
PreAppTasks::copyIniToDestDir();
....
return a.exec();
} -
@J-Hilk said in Save values to a config file:
//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, 16:52 last edited by@SGaist Yes, I've seen that, but I want to know where to put the code before.
//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")); }
That code
-
@SGaist Yes, I've seen that, but I want to know where to put the code before.
//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")); }
That code
@Sucharek said in Save values to a config file:
Yes, I've seen that, but I want to know where to put the code before.
//From to create class PreAppTasks:
-> Functions/Code taken from a class that you yourself will have to create. In this case I originally named it PreAppTasks. That is why you will find the class Prefix
PreAppTasks
before the the function bodies. You can, but you don't have to use the same class name, that is up to your discretion....
Was a bit long for a comment so I shortened it. -
@Sucharek said in Save values to a config file:
Yes, I've seen that, but I want to know where to put the code before.
//From to create class PreAppTasks:
-> Functions/Code taken from a class that you yourself will have to create. In this case I originally named it PreAppTasks. That is why you will find the class Prefix
PreAppTasks
before the the function bodies. You can, but you don't have to use the same class name, that is up to your discretion....
Was a bit long for a comment so I shortened it.wrote on 8 Sept 2021, 17:09 last edited by J.Hilk 9 Sept 2021, 06:00@J-Hilk Ok, so I created a new class (PreAppTasks) and put in the code, but I have some errors:
out-of-line definition of 'copyIniToDestDir' does not match any declaration in 'PreAppTasks'
use of undeclared identifier 'getDestIniFile'
unknown type name 'File'
out-of-line definition of 'getDestIniFile' does not match any declaration in 'PreAppTasks'my preapptasks.cpp
#include "preapptasks.h" #include <QFile> #include <QFileInfo> #include <QDir> #include <QStandardPaths> PreAppTasks::PreAppTasks() { } void PreAppTasks::copyIniToDestDir() { QFile *destFile = getDestIniFile(); //QFile *orgFile = getOrgIniFile(); //in your case simply: QFile *orgFile = new QFile(":/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")); }
-
@J-Hilk Ok, so I created a new class (PreAppTasks) and put in the code, but I have some errors:
out-of-line definition of 'copyIniToDestDir' does not match any declaration in 'PreAppTasks'
use of undeclared identifier 'getDestIniFile'
unknown type name 'File'
out-of-line definition of 'getDestIniFile' does not match any declaration in 'PreAppTasks'my preapptasks.cpp
#include "preapptasks.h" #include <QFile> #include <QFileInfo> #include <QDir> #include <QStandardPaths> PreAppTasks::PreAppTasks() { } void PreAppTasks::copyIniToDestDir() { QFile *destFile = getDestIniFile(); //QFile *orgFile = getOrgIniFile(); //in your case simply: QFile *orgFile = new QFile(":/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")); }
wrote on 8 Sept 2021, 19:28 last edited by@Sucharek said in Save values to a config file:
my preapptasks.cpp
So how does this compare to what is in your
preapptasks.h
? -
@Sucharek said in Save values to a config file:
my preapptasks.cpp
So how does this compare to what is in your
preapptasks.h
? -
wrote on 8 Sept 2021, 19:54 last edited by
-
wrote on 8 Sept 2021, 20:27 last edited by
@JonB Ok, so I added
copyIniToDestDir();
getDestIniFile();
to the .h file, but I'm not sure what to declare there (err message: C++ requires a type specifier for all declarations).
I'm also having a problem with declaring the File variable.
On
QFile *orgFile = new File(":/config/config.ini");
when I try to declare it in the .h file (QFile File;), it doesn't fix the error message (unknown type name 'File'). -
@JonB Ok, so I added
copyIniToDestDir();
getDestIniFile();
to the .h file, but I'm not sure what to declare there (err message: C++ requires a type specifier for all declarations).
I'm also having a problem with declaring the File variable.
On
QFile *orgFile = new File(":/config/config.ini");
when I try to declare it in the .h file (QFile File;), it doesn't fix the error message (unknown type name 'File'). -
wrote on 9 Sept 2021, 05:19 last edited by
Hi, so I've looked into it, and fixed almost all errors, except the "unknown type name 'File'".
I still can't figure out how to fix that one. -
Hi, so I've looked into it, and fixed almost all errors, except the "unknown type name 'File'".
I still can't figure out how to fix that one.@Sucharek thats actually a typo, and I now see, thats actually from my original code, I blame auto correction:
QFile *orgFile = new File(":/config/config.ini");
QFile *orgFile = newQFile
(":/config/config.ini"); -
wrote on 9 Sept 2021, 09:50 last edited by
Hi, ok, when I get home, I'll try it.
25/43