This is another one of those "which way is best" questions. I am wondering which is the best way to use named constant QStrings in a class. For example:
class MyClass {
public:
void doStuff() {
{
//do all sorts of database stuff here
db.close();
}
}
};
class MyClass {
public:
void doStuff() {
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "MySpecialDbName");
//do all sorts of database stuff here
db.close();
}
QSqlDatabase::removeDatabase("MySpecialDbName");
}
};
To copy to clipboard, switch view to plain text mode
"MySpecialDbName" should be a named constant, right? Especially if doStuff() is complex or the call to removeDatabase() is done in another method etc.
One way I thought of is to use an enum and a function to translate the enum to a QString.
enum MyStrings {
DbName,
SettingsGroup,
ProgramName
};
QString getMyString
(MyStrings string
) { switch (string) {
case (DbName):
case (SettingsGroup):
case (ProgramName):
default:
}
}
enum MyStrings {
DbName,
SettingsGroup,
ProgramName
};
QString getMyString(MyStrings string) {
switch (string) {
case (DbName):
return QString("MySpecialDbName");
case (SettingsGroup):
return QString("my/config/db");
case (ProgramName):
return QString("My Program");
default:
return QString();
}
}
To copy to clipboard, switch view to plain text mode
Or even the enum as above and an array of QStrings or a QStringList.
Maybe I'm just being pedantic but I can't seem to find any examples of what other people do. And maybe it doesn't matter in the end anyway... but I figured I'd ask.
Bookmarks