QSettings can't create .ini, but can read it O_o
-
Hi,
first, I did not see you read value in constructor ... but, except that you still have mistakes in code and no, you have not done what SGaist said:
- stop using findChild whenever and everywhere :) especially when you have access to a class members and you have just created them
this line:
@ findChild<QLCDNumber*>()->display(settings->value("clics").toInt());@
is awkward: findChild<QLCDNumber*>()=clicks object , and settings->value read out again an unmodified value
anyway delete it at all :)
if you need dynamic storage call setValue after
@clicks->display(clicks->intValue()+1);@
but will slow the code execution ... setValue in destructor is enough as it's been said already
- stop using findChild whenever and everywhere :) especially when you have access to a class members and you have just created them
-
Ok thank you a lot for these useful pieces of advice,I removed the findchilds and everything , but actually, what do you mean in "read values"? I tried out something: adding a setValue and Value in the constructor, but nothing happens
Thanks for your patience, many thanks.. -
Don't delete widgets/layout like that in your constructor. It's already all managed for you by Qt.
What do you mean by nothing happens ?
-
Wait what!! I found something awesome: I disabled Avast! and it started working!! Awesome you will say, but look at that:
@settings->setPath(QSettings::IniFormat,QSettings::UserScope,(QDir::currentPath() + QString("/test.ini")));
//.....
qDebug() << (QDir::currentPath() + QString("/test.ini"));@But still no test.ini ?! Where the heck is it?
If you wanna know where it is.. "C:/Users/LOUISPAUL/Projets/Qt/Clic_Le_Clown-build-desktop/test.ini"
here you go.. -
Where were you looking before ?
Since you use QDir::currentPath() the file would logically be created where the executable exists.
As for why Avast is interfering... Good question...
-
Today everything changed:
- I found out that the program starts up with a value of 15
- I can't write in the .ini
- The ini is still missing
- I updated my code, made sure all antiviruses are disabled
@#include "Clown.h"
#include <QDebug>Clown::Clown(QWidget *parent) :
QWidget(parent)
{
sound = new QSound(QString("pouet.wav"),this);
QVBoxLayout *lay = new QVBoxLayout(this);
settings = new QSettings(QSettings::IniFormat, QSettings::UserScope,"Paul Combaldieu", "Clic Le Clown");
settings->setPath(QSettings::IniFormat,QSettings::UserScope,(QDir::currentPath() + QString("/test.ini")));// settings->setValue("test", 9000);
// settings->value("test"); //does nothing
// settings->remove(QString("test"));qDebug() << (QDir::currentPath() + QString("/test.ini")); qDebug() << settings->value("clics").toInt(); clicks = new QLCDNumber; clicks->setStyleSheet(QString("color: red;")); QPushButton *but = new QPushButton; but->setIcon(QIcon("Clown.jpg")); but->setIconSize(QSize(200,200)); clicks->display(settings->value("clics").toInt()); lay->addWidget(clicks); lay->addWidget(but); setLayout(lay); setMinimumSize(300,300); QObject::connect(but,SIGNAL(clicked()),this,SLOT(PlusOne()));
}
void Clown::PlusOne()
{
if(!sound->isFinished())
sound->stop();clicks->display(clicks->intValue()+1); findChild<QPushButton*>()->setFocus(); sound->play();
}
Clown::~Clown()
{
qDebug() << "In destructor, writing " << clicks->intValue() << "in ini file.";
settings->setValue("clics",clicks->intValue());
}
@Log:
@Starting de C:\Users\LOUISPAUL\Projets\Qt\Clic_Le_Clown-build-desktop\debug\Clic_Le_Clown.exe...
"C:/Users/LOUISPAUL/Projets/Qt/Clic_Le_Clown-build-desktop/test.ini"
15
In destructor, writing 23 in ini file.
C:\Users\LOUISPAUL\Projets\Qt\Clic_Le_Clown-build-desktop\debug\Clic_Le_Clown.exe terminated code 0Starting C:\Users\LOUISPAUL\Projets\Qt\Clic_Le_Clown-build-desktop\debug\Clic_Le_Clown.exe...
"C:/Users/LOUISPAUL/Projets/Qt/Clic_Le_Clown-build-desktop/test.ini"
15
In destructor, writing 17 in ini file.
C:\Users\LOUISPAUL\Projets\Qt\Clic_Le_Clown-build-desktop\debug\Clic_Le_Clown.exe terminated with code 0@Note that in the path, the /debug folder is missing before test.ini
-
QDir::currentPath() returns the path of the directory containing the executable so it's points to the folder containing debug.
You don't delete settings in your destructor so it is not properly destroyed.
-
Wait so does it mean that I don't have to delete my widgets in the destructor, but I have to delete the QSettings? Why?
Edit
Just found out something:
@settings = new QSettings(QSettings::IniFormat, QSettings::UserScope,"Paul Combaldieu", "Clic Le Clown");
settings->setPath(QSettings::IniFormat,QSettings::UserScope,(QDir::currentPath() + QString("test.ini")));
//Later
qDebug() << settings->fileName();@filename: "C:/Users/LOUISPAUL/AppData/Roaming/Paul Combaldieu/Clic Le Clown.ini"
Wtf?
-
The widgets inside your Clown widget become children of Clown once you put them in the layout that is also a child of Clown. Your settings variable is not a child so you have to delete it manually.
setPath is a static function that sets the path (folder) where the files of format IniFormat will be stored. Since you call it after having created your settings object it wont affect it.
The file name and location are build based on what you give in the constructor. "Paul Combaldieu" as the company and "Clic Le Clown" as the application name.
The settings files are generally stored in the "application name" folder found in the "company name" folder.