QSettings verify if file exists



  • 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?


  • Moderators

    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.



  • 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";
    @



  • 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



  • 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]



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



  • In

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

    Clipper.conf is automatically created if does not exist?


  • Moderators

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



  • 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");
    @



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



  • 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


  • 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?


  • Moderators

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.