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. Global functions?
Forum Updated to NodeBB v4.3 + New Features

Global functions?

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

    I was thinking of creating a global QSettings so every files can just use the same QSettings object? I know global variables can be defined as extern and used as global variables. But, how do i define global functions to be used? Can't find a solution anywhere.

    jsulmJ 1 Reply Last reply
    0
    • G GCDX

      I was thinking of creating a global QSettings so every files can just use the same QSettings object? I know global variables can be defined as extern and used as global variables. But, how do i define global functions to be used? Can't find a solution anywhere.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @GCDX There is no such thing as "global function". What you want is simply a function declared in a header file and defined somewhere in a cpp file.

      // header xyz.h
      int doSomething();
      
      // xyz.cpp
      #include "xyz.h"
      int doSomething()
      {
          return 0;
      }
      

      Now include xyz.h where you want to use the function

      You should read http://doc.qt.io/qt-5/qsettings.html once more as there is no need for a "global" QSettings:
      "If you use QSettings from many places in your application, you might want to specify the organization name and the application name using QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName(), and then use the default QSettings constructor:"

      // You need to execute following 3 lines once in your app
      QCoreApplication::setOrganizationName("MySoft");
      QCoreApplication::setOrganizationDomain("mysoft.com");
      QCoreApplication::setApplicationName("Star Runner");
      ...
      // Then create QSettings instance where and when needed
      QSettings settings;
      

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

      1 Reply Last reply
      3
      • KillerSmathK Offline
        KillerSmathK Offline
        KillerSmath
        wrote on last edited by
        #3

        @GCDX
        What do you think about Singleton design pattern ?

        The singleton pattern is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

        Read more:
        https://sourcemaking.com/design_patterns/singleton

        @Computer Science Student - Brazil
        Web Developer and Researcher
        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

        jsulmJ kshegunovK 2 Replies Last reply
        0
        • KillerSmathK KillerSmath

          @GCDX
          What do you think about Singleton design pattern ?

          The singleton pattern is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

          Read more:
          https://sourcemaking.com/design_patterns/singleton

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

          @KillerSmath Singleton should be avoided for many reasons. For QSettings there is no need for a singleton as creating QSettings instance after configuring is cheap.

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

          KillerSmathK 1 Reply Last reply
          2
          • jsulmJ jsulm

            @KillerSmath Singleton should be avoided for many reasons. For QSettings there is no need for a singleton as creating QSettings instance after configuring is cheap.

            KillerSmathK Offline
            KillerSmathK Offline
            KillerSmath
            wrote on last edited by
            #5

            @jsulm said in Global functions?:

            @KillerSmath Singleton should be avoided for many reasons. For QSettings there is no need for a singleton as creating QSettings instance after configuring is cheap.

            Yes, yes, it was my mistake, i forgot the Qsettings unique file save model

            @Computer Science Student - Brazil
            Web Developer and Researcher
            “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

            JonBJ 1 Reply Last reply
            0
            • KillerSmathK KillerSmath

              @GCDX
              What do you think about Singleton design pattern ?

              The singleton pattern is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

              Read more:
              https://sourcemaking.com/design_patterns/singleton

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @KillerSmath said in Global functions?:

              @GCDX
              What do you think about Singleton design pattern ?

              http://lengthily.blogspot.com/2017/02/one-size-fits-all.html

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              2
              • KillerSmathK Offline
                KillerSmathK Offline
                KillerSmath
                wrote on last edited by
                #7

                @kshegunov
                Thank you for the support material. Everyday i'm learning something new and improving some of my concepts about the programming world :)

                @Computer Science Student - Brazil
                Web Developer and Researcher
                “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                1 Reply Last reply
                1
                • KillerSmathK KillerSmath

                  @jsulm said in Global functions?:

                  @KillerSmath Singleton should be avoided for many reasons. For QSettings there is no need for a singleton as creating QSettings instance after configuring is cheap.

                  Yes, yes, it was my mistake, i forgot the Qsettings unique file save model

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @jsulm

                  as there is no need for a "global" QSettings:
                  For QSettings there is no need for a singleton as creating QSettings instance after configuring is cheap.

                  I presently do create a single "global" object for my QSettings to guarantee "efficiency". I have questions about changing over to creating new instances when needed.

                  I am happy to call QCoreApplication::setOrganizationName/setOrganizationDomain/setApplicationName() once.

                  I use QSettings::IniFormat unconditionally on both Windows & Linux. I may wish to specify the filename/path (undecided).

                  Constructing a QSettings object (for reading settings, dotted all over my code) is only "cheap" provided somewhere Qt has cached the (parsed) content once and knows when to re-use that for all sundry QSettings settings statements. Otherwise it's "expensive" (IMO).

                  What exactly do I need to do/call to keep being able to construct new QSettings objects "cheaply"? I don't see where docs explain what exactly will be cached. For example:

                  • Is every call to constructor QSettings::QSettings(const QString &fileName, QSettings::Format format, QObject *parent = nullptr) guaranteed to fetch a cached object for every unique combination of fileName & format?
                  • Does calling QSettings::setDefaultFormat(QSettings::Format format) instead of specifying it in constructor give me caching for that?
                  • Similarly, does calling QSettings::setPath(QSettings::Format format, QSettings::Scope scope, const QString &path) instead of each time in constructor give me a caching for the combination?
                  • Finally, I note that if I need QSettings::setIniCodec() that is an instance function and not static. I would have to call that on each created instance, and the implication (to me) is that it would require the file to be re-read each time.

                  If by any chance QSettings is not doing any of this caching then I think the suggestion that QSettings settings; is "cheap" is very misleading....

                  jsulmJ kshegunovK 2 Replies Last reply
                  0
                  • JonBJ JonB

                    @jsulm

                    as there is no need for a "global" QSettings:
                    For QSettings there is no need for a singleton as creating QSettings instance after configuring is cheap.

                    I presently do create a single "global" object for my QSettings to guarantee "efficiency". I have questions about changing over to creating new instances when needed.

                    I am happy to call QCoreApplication::setOrganizationName/setOrganizationDomain/setApplicationName() once.

                    I use QSettings::IniFormat unconditionally on both Windows & Linux. I may wish to specify the filename/path (undecided).

                    Constructing a QSettings object (for reading settings, dotted all over my code) is only "cheap" provided somewhere Qt has cached the (parsed) content once and knows when to re-use that for all sundry QSettings settings statements. Otherwise it's "expensive" (IMO).

                    What exactly do I need to do/call to keep being able to construct new QSettings objects "cheaply"? I don't see where docs explain what exactly will be cached. For example:

                    • Is every call to constructor QSettings::QSettings(const QString &fileName, QSettings::Format format, QObject *parent = nullptr) guaranteed to fetch a cached object for every unique combination of fileName & format?
                    • Does calling QSettings::setDefaultFormat(QSettings::Format format) instead of specifying it in constructor give me caching for that?
                    • Similarly, does calling QSettings::setPath(QSettings::Format format, QSettings::Scope scope, const QString &path) instead of each time in constructor give me a caching for the combination?
                    • Finally, I note that if I need QSettings::setIniCodec() that is an instance function and not static. I would have to call that on each created instance, and the implication (to me) is that it would require the file to be re-read each time.

                    If by any chance QSettings is not doing any of this caching then I think the suggestion that QSettings settings; is "cheap" is very misleading....

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @JonB Well, I don't know whether QSettings caches anything. The question is: how often do you read/write settings? If this is something you do often in your app I would consider writing a wrapper class which reads the settings once and writes changed values only if you ask it to do so. This way you have more control, but you would need to make sure you use same instance everywhere.

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

                    JonBJ 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @JonB Well, I don't know whether QSettings caches anything. The question is: how often do you read/write settings? If this is something you do often in your app I would consider writing a wrapper class which reads the settings once and writes changed values only if you ask it to do so. This way you have more control, but you would need to make sure you use same instance everywhere.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @jsulm
                      Reading settings is/may be dotted randomly all over the code (not my code, previous authors felt they could read a setting whenever it suited them, all sorts of stuff is saved in settings file).

                      I agree totally then about "make sure you use same instance everywhere", but that indeed takes us back to the opposite recommendation given to the OP! Even if it's wrapped in a convenience class, that class needs a global instance or singleton.

                      Which is where the OP started from! Unless QSettings promises some kind of caching (at least for my choice of QSettings::IniFormat), I don't mean to sound rude but suggesting that you can get away without global/singleton and just create instances is "misleading" or "requires a qualified explanation" IMHO. [If you only access the settings once in code, you may as well not bother with the static initialization functions like QCoreApplication::setOrganizationName and you won't go QSettings settings more than once anyway.]

                      jsulmJ 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @jsulm
                        Reading settings is/may be dotted randomly all over the code (not my code, previous authors felt they could read a setting whenever it suited them, all sorts of stuff is saved in settings file).

                        I agree totally then about "make sure you use same instance everywhere", but that indeed takes us back to the opposite recommendation given to the OP! Even if it's wrapped in a convenience class, that class needs a global instance or singleton.

                        Which is where the OP started from! Unless QSettings promises some kind of caching (at least for my choice of QSettings::IniFormat), I don't mean to sound rude but suggesting that you can get away without global/singleton and just create instances is "misleading" or "requires a qualified explanation" IMHO. [If you only access the settings once in code, you may as well not bother with the static initialization functions like QCoreApplication::setOrganizationName and you won't go QSettings settings more than once anyway.]

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @JonB In my opinion it really depends on the app. Even if you read/write settings at many places - is it really performance relevant? Do you read/write settings in a long lasting loop? How often do you read/write settings? Why do you think it is important to have some caching in QSettings? Don't forget it would increase RAM consumption - for what? Don't over-engineer.

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

                        JonBJ 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @jsulm

                          as there is no need for a "global" QSettings:
                          For QSettings there is no need for a singleton as creating QSettings instance after configuring is cheap.

                          I presently do create a single "global" object for my QSettings to guarantee "efficiency". I have questions about changing over to creating new instances when needed.

                          I am happy to call QCoreApplication::setOrganizationName/setOrganizationDomain/setApplicationName() once.

                          I use QSettings::IniFormat unconditionally on both Windows & Linux. I may wish to specify the filename/path (undecided).

                          Constructing a QSettings object (for reading settings, dotted all over my code) is only "cheap" provided somewhere Qt has cached the (parsed) content once and knows when to re-use that for all sundry QSettings settings statements. Otherwise it's "expensive" (IMO).

                          What exactly do I need to do/call to keep being able to construct new QSettings objects "cheaply"? I don't see where docs explain what exactly will be cached. For example:

                          • Is every call to constructor QSettings::QSettings(const QString &fileName, QSettings::Format format, QObject *parent = nullptr) guaranteed to fetch a cached object for every unique combination of fileName & format?
                          • Does calling QSettings::setDefaultFormat(QSettings::Format format) instead of specifying it in constructor give me caching for that?
                          • Similarly, does calling QSettings::setPath(QSettings::Format format, QSettings::Scope scope, const QString &path) instead of each time in constructor give me a caching for the combination?
                          • Finally, I note that if I need QSettings::setIniCodec() that is an instance function and not static. I would have to call that on each created instance, and the implication (to me) is that it would require the file to be re-read each time.

                          If by any chance QSettings is not doing any of this caching then I think the suggestion that QSettings settings; is "cheap" is very misleading....

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #12

                          QSettings will create global objects internally (thus the documentation). So when the program is ending the cleanup routines will call sync() and will flush the changes. Also you can call sync() manually to flush the changes. If I recall correctly, the settings object will cache the read data and they will also monitor the settings file for changes (due to other settings objects of other application(s) writing something) and update the cached information.

                          I actually don't like how the settings work in principle, I'd very much have liked this to be left to the user code.

                          This should give an answer to your other concerns.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          4
                          • jsulmJ jsulm

                            @JonB In my opinion it really depends on the app. Even if you read/write settings at many places - is it really performance relevant? Do you read/write settings in a long lasting loop? How often do you read/write settings? Why do you think it is important to have some caching in QSettings? Don't forget it would increase RAM consumption - for what? Don't over-engineer.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            @jsulm
                            We'll agree to differ then :) Do I (rather the code) read settings in a loop? Who knows? No reason why it shouldn't. Especially when I'm just told "creating a new QSettings object is cheap", but nothing is said about the cost of then going QSettings::value() which is what I'm going to want to do. IMHO anything which might involve repeatedly opening a file, reading it and then parsing it is hugely expensive time-wise (RAM-wise, one common INI file content is not big).... Anyway, for me if not necessarily the OP, the moral is I do want a single, permanent object.

                            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