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. How to set a global constant?
Forum Updated to NodeBB v4.3 + New Features

How to set a global constant?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 3.8k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    KelvinSP
    wrote on last edited by KelvinSP
    #1

    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

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

      Hi,

      Why do you need that constant for ?

      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
      1
      • K Offline
        K Offline
        KelvinSP
        wrote on last edited by KelvinSP
        #3

        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

        jsulmJ 1 Reply Last reply
        0
        • K KelvinSP

          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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          6
          • K Offline
            K Offline
            KelvinSP
            wrote on last edited by KelvinSP
            #5

            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.

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

              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.

              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
              2
              • K Offline
                K Offline
                KelvinSP
                wrote on last edited by KelvinSP
                #7

                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.

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

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  K 1 Reply Last reply
                  3
                  • SGaistS SGaist

                    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.

                    K Offline
                    K Offline
                    KelvinSP
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    1

                    • Login

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