Qt World Summit: Submit your Presentation

Global functions?

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

  • Lifetime Qt Champion

    @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::setApplicationName("Star Runner");
    // Then create QSettings instance where and when needed
    QSettings settings;

  • @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:

  • Lifetime Qt Champion

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

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

  • Moderators

    @KillerSmath said in Global functions?:

    What do you think about Singleton design pattern ?


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

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

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion

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

  • Moderators

    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.

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

Log in to reply