0

I'm trying to make a global settings provider for an application. It seemed bulky to have one object duplicated in so many different classes.

I've seeth this method work when the private static variable was something simple, like an integer, but I want it to work for QSettings—an object.

// settings.h
class Settings {
public:
    static void Initialize();
    static int serverRefreshRate();

private:
    QSettings *settings;
};

// settings.cpp
#include "Server/settings.h"

void Settings::Initialize() {
    Settings::settings = new QSettings(/* etc */);
}

int Settings::serverRefreshRate() {
    return settings->value("server/refreshRate", 10000).toInt();
}

Is there a way I can achieve this, or am I going about it in the wrong way?

Thanks!

EDIT,

Firstly, it needed to be:

static QSettings *settings;

And I needed the following in the .cpp.

QSettings* Settings::settings = NULL;
Jacob
  • 1,335
  • 1
  • 14
  • 28
  • 3
    The problem here is that settings isn't static. I'd make the whole Settings class a singleton instead of using static methods. – Frank Osterfeld Jan 17 '14 at 15:01
  • @FrankOsterfeld That's what I answered to his previous question here!: https://stackoverflow.com/questions/21187340/best-way-to-handle-system-wide-qsettings-in-multiple-classes – TheDarkKnight Jan 17 '14 at 15:09
  • You did. I thought I might be able to get away without doing a singleton. :P – Jacob Jan 17 '14 at 15:12
  • 1
    A singleton is probably the simplest of all design patterns. Why are you trying to avoid it? – TheDarkKnight Jan 17 '14 at 15:13
  • Am I right in thinking that my implementation will read Settings::instance()->serverRefreshRate() ? If so, I would have rathered just Settings::serverRefreshRate(). It's not too important, and it is working now. – Jacob Jan 17 '14 at 15:28
  • 2
    you can wrap the singleton dereferencing in static methods. It's definitely easier to manage one singleton instead of constructing/deconstructing multiple static members correctly and safely. – Frank Osterfeld Jan 17 '14 at 15:38
  • Yes, you would write Settings::instance()->serverRefreshRate. By using all static variables and functions, you'll have much more code to manage when adding and changing any variables in the class. – TheDarkKnight Jan 17 '14 at 15:40

0 Answers0