How to set a global constant?



  • Hello, I need to set the path of my database file as a global constant. It needs to be set in the main.cpp file and be available for any class in the application. How can I properly do that?

    I was thinking in use the property option from the QObject , for example:

    qApp->setProperty("DATABASE_PATH", "C:/../Database.db");
    QString dbPath = qApp->property("DATABASE_PATH").toString();
    

    But I don't know if it is the best/correct solution.

    Thanks


  • Lifetime Qt Champion

    Hi,

    Why do you need that constant for ?



  • Hi, I need to set the path for the database file so I can use it in two classes, one to handle the database connection and another only used to copy the database file.

    For example, set this as global:

    QString globalDbPath = "C:/Database.db";
    

    And then I can use it as:

    // class 1
    
    dbClass.connect(globalDbPath);
    
    // class 2
    
    copy.database(globalDbPath);
    

    So I don't need to set the database path in each class and if I need to change the database name I can do it only in one place.

    I know that use global constants or variables is not recommended, so I'm asking how to achieve this in a proper way? Should I still use global const or Qt provides some other way that I can use to do that (for example, the property option)?

    Note that this is just an example, but I also need to do that with a path for the settings file (.ini).

    Thanks


  • Moderators

    @KelvinSP You should avoid global const and variables as much as possible. The access to the database should be limited to as few classes as possible. Why should many classes have access to the database? You should have only one class handling the database access, then there is no need for a global const. This class can provide an interface to read/write data from/to database in a way the other classes even do not know how the data is actually stored.



  • Thanks @jsulm. I know that only one class should handle the database access, and I have only one class to do this. Actually, I want to use it only in two classes, one class that handle the database access and another that is used only to create a copy of the database file, for export purposes. This is just an example because I also need to store the path to the settings file (.ini), that is used in about 5 classes to save/load some values.

    Summarizing: What I need is only a way to declare a QString and make it available for all classes in the application.

    For example:

    QString DATABASE_PATH = "C:/../Database.db";
    QString SETTINGS_PATH = "C:/../Settings.ini";
    

    I have updated the answer above, please take a look.


  • Lifetime Qt Champion

    Still no need for multiple copy of that variable. The export should also be handled by the database manager class.

    Same goes for your application settings. Why not create one class that handle the settings rather than multiplying code that will mostly do the same thing.



  • The database class is responsible for handling the database access (connect, insert, select, etc), I don't think the export option should be managed by this class (note that it is not exporting the data itself, it is only copying the .db file), but I can provide a function to get the database path from this class easily. This is just my opinion, but I'm a beginner on Qt and C++.

    So, for the settings file, I have 5 "pages" (QWidgets) in my application that uses the settings option like this:

    void MyClass::saveSettings()
    {
        QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
        QSettings *settings = new QSettings(path + "/Settings.ini", QSettings::IniFormat);
    
        settings->beginGroup("SettingsPage1");
        settings->setValue("Data1", data1->value());
        settings->setValue("Data2", data2->value());
        settings->setValue("Data3", data3->value());
        settings->setValue("Data4", data4->value());
        settings->endGroup();
    
        delete settings;
        settings = NULL;
    }
    
    
    void MyClass::loadSettings()
    {
        QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
        QSettings *settings = new QSettings(path + "/Settings.ini", QSettings::IniFormat);
    
        settings->beginGroup("SettingsPage1");
        data1->setValue( settings->value("Data1", 0.00).toDouble() );
        data2->setValue( settings->value("Data2", 0.00).toDouble() );
        data3->setValue( settings->value("Data3", 0.00).toDouble() );
        data4->setValue( settings->value("Data4", 0.00).toDouble() );
        settings->endGroup();
    
        delete settings;
        settings = NULL;
    }
    

    Note that I had put this functions inside the class that handles the GUI (of each page) because I need to access the fields (widgets) of that form.

    Note: data1, data2, etc, are double spin boxes.

    What I was looking for is just a global path to replace the local path + "/Settings.ini" so, if I need to change the settings file name or path I can do it just in one place in the code.

    As you said I can create one class that handle the settings rather than multiplying code, but how can I do that taking into account that I need to access the GUI? Sure I can do it in several ways, but I don't know how can I do this in a simple/clean way (simpler than the two functions shown above)?

    Thanks @SGaist.


  • Lifetime Qt Champion

    Since these are constants, you can create a small sub-class of QSettings, that initialises it the way you need it.

    On a side note, there's no need to new a QSettings object like you do since you only use it in that function. Just create the object on the stack.



  • Thanks @SGaist, understood, I will try it out.


Log in to reply
 

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