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. QSettings can't create .ini, but can read it O_o

QSettings can't create .ini, but can read it O_o

Scheduled Pinned Locked Moved General and Desktop
19 Posts 4 Posters 6.0k Views
  • 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.
  • N Offline
    N Offline
    NicuPopescu
    wrote on last edited by
    #10

    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

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yhu420
      wrote on last edited by
      #11

      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..

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #12

        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 ?

        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
        • Y Offline
          Y Offline
          yhu420
          wrote on last edited by
          #13

          Hmm destructor you mean, right?
          I mean by nothing happens that there's still no .ini file, I qDebugged the place where the file was supposed to be created, but there's still nothing..

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            yhu420
            wrote on last edited by
            #14

            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..

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #15

              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...

              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
              • Y Offline
                Y Offline
                yhu420
                wrote on last edited by
                #16

                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 0

                Starting 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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #17

                  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.

                  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
                  • Y Offline
                    Y Offline
                    yhu420
                    wrote on last edited by
                    #18

                    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?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #19

                      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.

                      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

                      • Login

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