PDA

View Full Version : Put all labels in an external file, which pattern to use ?



oswalidos
18th May 2012, 11:41
Hello,

I want to put all messages and labels, sql queries ... in external files so i won't dive in the code to change a label or an url later, i think i'll use Qsettings, now the problem is :
1) is it good (http://c2.com/cgi/wiki?GlobalVariablesAreBad) to make Qsettings instance as global instance using Q_GLOBAL_STATIC ? i think it's bad it. (actually target platforms are mac and windows but laters it'll be mobile, so probably performance is important)
2) I have an idea that may appear weird : to make all my classes which need Qsettings instance inherits from a class that contains qsettings instance ready for use, if it's bad, why ?

I googled for some hours and i didn't find a good solution, i found someone trying to implement dependency injection by editing the singleton pattern but honestly i didn't get it,

There is certainly many who faced the same problem, would you tell me please what is the best approach ?

Thanks in advance.

ChrisW67
18th May 2012, 21:46
I think you are trying to solve a problem that does not exist. You create a QSettings object when you need it, use it, and discard it. There is no need to keep a QSettings instance in a global variable. You can set some common values used by QSettings once (usually in main) to make using QSettings elsewhere trivial.


int main(...) {
QApplication app(argc, argv);

QCoreApplication::setOrganizationName("MySoft");
QCoreApplication::setOrganizationDomain("mysoft.com");
QCoreApplication::setApplicationName("Star Runner");

...
}

// elsewhere
void foo() {
QSettings settings;
QString label = settings.value("Label", "Default value").toString();
}

void bar() {
QSettings settings;
QString label = settings.value("OtherLabel", "Default value").toString();
}


QSettings is for storing relatively small amounts of information.

The Qt translation mechanism may be a better way to handle the label text if you are trying to achieve for example French/Arabic versions of the labels.