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 verify if file exists
QtWS25 Last Chance

QSettings verify if file exists

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 16.3k 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.
  • E Offline
    E Offline
    Exotic_Devel
    wrote on last edited by
    #1

    I am using QSettings to store the connection parameters to the database. When the application is first run, no settings file exists, then I have to open a dialog for the user to enter the connection parameters.

    How to verify that the file exists in portable mode?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You can check if a key already exists. Checkout "value":http://qt-project.org/doc/qt-5/qsettings.html#value

      I am typically using a special default value for a key. You can even use this for individual parameters, but if you have one parameter which has to be there, you can use it or introduce one for this testing only.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        You could do as koahnig said by checking if the parameter exists by setting a default value, or you can use QFile to see if the actual file exists on the file system.

        @
        if (QFile(fileName).exists())
        qDebug() << "File exists";
        else
        qDebug() << "File does not exist";
        @

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Exotic_Devel
          wrote on last edited by
          #4

          I can not use default values​​. Parameters such as username, password, ip-host, must be entered by the user in the first run of the application.
          Connection parameters are generally dynamic, ie depend user

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Exotic_Devel
            wrote on last edited by
            #5

            I still do not understand how the file is named, for example:

            @void MainWindow::writeSettings()
            {
            QSettings settings("Moose Soft", "Clipper");

            settings.beginGroup("MainWindow");
            settings.setValue("size", size());
            settings.setValue("pos", pos());
            settings.endGroup();
            

            }
            @

            What is the filename?

            [quote author="dvez43" date="1407786276"]You could do as koahnig said by checking if the parameter exists by setting a default value, or you can use QFile to see if the actual file exists on the file system.

            @
            if (QFile(fileName).exists())
            qDebug() << "File exists";
            else
            qDebug() << "File does not exist";
            @[/quote]

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #6

              [quote author="Exotic_Devel" date="1407786830"]What is the filename?[/quote]
              I think it will be hard to find a file name for QSettings because it uses different files on "different systems":http://qt-project.org/doc/qt-5/QSettings.html#platform-specific-notes

              [quote author="Exotic_Devel" date="1407786512"]I can not use default values​​. Parameters such as username, password, ip-host, must be entered by the user in the first run of the application.
              Connection parameters are generally dynamic, ie depend user[/quote]

              In addition to koahnig suggestion. You can use "QSettings::contains()":http://qt-project.org/doc/qt-5/qsettings.html#contains to check if key exists. And if it does not then emit a signal to request user input. When user enter the data write it to QSettings and next time you will read it.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Exotic_Devel
                wrote on last edited by
                #7

                In

                @QSettings settings("MooseSoft", "Clipper");
                if(settings.contains("anykey"))
                qDebug("yes");
                else
                qDebug("no");@

                Clipper.conf is automatically created if does not exist?

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  [quote author="Exotic_Devel" date="1407791525"]In

                  @QSettings settings("MooseSoft", "Clipper");
                  if(settings.contains("anykey"))
                  qDebug("yes");
                  else
                  qDebug("no");@

                  Clipper.conf is automatically created if does not exist?[/quote]

                  The location, filename and/or format are OS dependent. However, in general QSettings ensures that the information is stored.
                  Not sure, if a file wil be created when no entry has been set, but that is irrelevant anyway.

                  Either you are using Andreyc's suggestion with contains or you may use an empty default string. When the return value is empty you know also that the parameter was not found.

                  As a remark to filenames. On windows you may store the information in registry. Therefore, the file name check is not always portable.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    Exotic_Devel
                    wrote on last edited by
                    #9

                    In my settings file, keys exist concerning the parameters of a connection to the database, ie, host, user, password, ...
                    In that case I would have to choose one of these keys to see if the file exists.
                    Bothers me that deciding whether a file exists based on the location of a key. The file can be there, but the key is incorrect.
                    I find it strange define whether a file exists analyzing its contents.
                    This should be done seeking the binary file, if conf or ini or another file is there, then the file exists.
                    QSettings should have a existsFile () method, which would return true if the file exist in the directory.

                    @ QSettings settings("MooseSoft", "Clipper");
                    if(settings.existsFile())
                    qDebug("yes");
                    else
                    qDebug("no");
                    @

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andreyc
                      wrote on last edited by
                      #10

                      [quote author="Exotic_Devel" date="1407846882"]QSettings should have a existsFile () method, which would return true if the file exist in the directory.[/quote]

                      Let me quote a previous answer :-)

                      [quote author="koahnig" date="1407833743"] As a remark to filenames. On windows you may store the information in registry. Therefore, the file name check is not always portable. [/quote]

                      On some OS all settings are in one storage, which can be spread among several files. So the file always exists regardless of an application settings like registry in Windows.

                      [quote author="Exotic_Devel" date="1407846882"]Bothers me that deciding whether a file exists based on the location of a key. The file can be there, but the key is incorrect.
                      I find it strange define whether a file exists analyzing its contents.
                      This should be done seeking the binary file, if conf or ini or another file is there, then the file exists.[/quote]

                      You don't need to decide if file exists. You need to decide if a particular key-value pair exists. And if it does not then ask user about the value.
                      If the value exists but incorrect then you try to use stored credentials and then ask user if the credentials are incorrect.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        msue
                        wrote on last edited by
                        #11

                        In case you want to see it written somewhere anyway, with QSettings settings("MooseSoft", "Clipper"):

                        • On Linux it will will written to the file ~/.config/MooseSoft/Clipper.conf
                        • On Windows it will be written to the registry: HKEY_CURRENT_USER\Software\MooseSoft\Clipper
                        1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          Exotic_Devel
                          wrote on last edited by
                          #12

                          Here is the solution that I created

                          @QHash DataAccess::readSettings()
                          {
                          QSettings connecsettings( QSettings::IniFormat, QSettings::SystemScope, "Freedom", "TecTracker");

                          QHash<QString, QString> paran;

                          connecsettings.beginGroup("Connection");

                          if(!connecsettings.contains("host"))
                          writeSettings(&connecsettings);

                          paran.insert("host", connecsettings.value("host", "127.0.0.1"));
                          paran.insert("port", connecsettings.value("port", 5432).toString());
                          paran.insert("database", connecsettings.value("database"));
                          paran.insert("user", connecsettings.value("user", "postgres"));
                          paran.insert("passw", connecsettings.value("passw", "postgres"));

                          connecsettings->endGroup();

                          return paran;
                          }@

                          Is it?

                          Is there a more practical way to pass these parameters for a QHash?

                          Is there any container that works with mixed types, in order to avoid conversions?

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            koahnig
                            wrote on last edited by
                            #13

                            Possibly you could store QVariant directly, if like that.

                            @
                            QHash < QString, QVariant > paran;

                            paran.insert("host", connecsettings.value("host", "127.0.0.1"));
                            paran.insert("port", connecsettings.value("port", 5432));
                            paran.insert("database", connecsettings.value("database"));
                            paran.insert("user", connecsettings.value("user", "postgres"));
                            paran.insert("passw", connecsettings.value("passw", "postgres"));
                            @

                            However, you will have to convert later on. Personally, I would leave it as is.

                            Vote the answer(s) that helped you to solve your issue(s)

                            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