Results 1 to 13 of 13

Thread: QString constants in a class

  1. #1
    Join Date
    May 2006
    Posts
    70
    Thanks
    12
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QString constants in a class

    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:

    Qt Code:
    1. class MyClass {
    2. public:
    3. void doStuff() {
    4. {
    5. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "MySpecialDbName");
    6. //do all sorts of database stuff here
    7. db.close();
    8. }
    9. QSqlDatabase::removeDatabase("MySpecialDbName");
    10. }
    11. };
    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.
    Qt Code:
    1. enum MyStrings {
    2. DbName,
    3. SettingsGroup,
    4. ProgramName
    5. };
    6. QString getMyString(MyStrings string) {
    7. switch (string) {
    8. case (DbName):
    9. return QString("MySpecialDbName");
    10. case (SettingsGroup):
    11. return QString("my/config/db");
    12. case (ProgramName):
    13. return QString("My Program");
    14. default:
    15. return QString();
    16. }
    17. }
    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.

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QString constants in a class

    There's a more elegant way. Actual static constant string variables. C++ doesn't make this pretty, but here goes:

    myclass.h
    Qt Code:
    1. class MyClass {
    2. private:
    3. static const QString DbName;
    4. };
    To copy to clipboard, switch view to plain text mode 

    myclass.cpp
    Qt Code:
    1. #include "myclass.h"
    2.  
    3. const QString MyClass::DbName = QString("MySpecialDbName");
    To copy to clipboard, switch view to plain text mode 
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3
    Join Date
    May 2006
    Posts
    70
    Thanks
    12
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString constants in a class

    Oh you're right, that ain't pretty. But maybe it's good enough. =)

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QString constants in a class

    I tend to do it this way mostly for the reason that header file remains clean:

    myclass.cpp
    Qt Code:
    1. #include "myclass.h"
    2.  
    3. static const QString DBNAME("MySpecialDbName");
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Red face Re: QString constants in a class

    are, for instance, the following two declarations
    Qt Code:
    1. static const QString strTable(QObject::trUtf8("xxxx")); // table "xxxx" unicode name
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. const static QString strTable(QObject::trUtf8("xxxx")); // table "xxxx" unicode name
    To copy to clipboard, switch view to plain text mode 

    identical?
    Qt 5.3 Opensource & Creator 3.1.2

  6. #6
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString constants in a class

    I tend to just go:
    Qt Code:
    1. #include "myclass.h"
    2. #define DBNAME "MyDatabase"
    To copy to clipboard, switch view to plain text mode 

    Not very C++y, but it works and its simple. I don't see any reason to use a static variable instead.

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QString constants in a class

    Quote Originally Posted by MarkoSan View Post
    are, for instance, the following two declarations identical?
    Yes, as far as I know, the meaning is same. With some older compilers you might get warnings with the latter one, though.

    Quote Originally Posted by pherthyl View Post
    Not very C++y, but it works and its simple. I don't see any reason to use a static variable instead.
    Of course macros have their own use, but *cough* macros are evil! At least compilers and debuggers come along with typed variables. Try googling for "const vs #define".. http://www.comeaucomputing.com/techtalk/#definevsconst
    J-P Nurmi

  8. #8
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QString constants in a class

    Quote Originally Posted by MarkoSan View Post
    are, for instance, the following two declarations
    Qt Code:
    1. static const QString strTable(QObject::trUtf8("xxxx")); // table "xxxx" unicode name
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. const static QString strTable(QObject::trUtf8("xxxx")); // table "xxxx" unicode name
    To copy to clipboard, switch view to plain text mode 

    identical?
    A small note on trUtf8:
    Warning: This method is reentrant only if all translators are installed before calling this method. Installing or removing translators while performing translations is not supported. Doing so will probably result in crashes or other undesirable behavior.
    Not only that, but strTable will probably remain untranslated since strTable's constructor is likely to have been called before your translators were installed.
    Why don't you just keep a
    static const char strTable[]="xxx";
    And call trUtf8() every time you access it? Then the user would be able to change localization at runtime.

  9. #9
    Join Date
    Oct 2007
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString constants in a class

    you could also think of using QSettings instead of the constant. You could hold the databasename and settings in an .ini file (also in the registry if you're on windows) and wouldn't have to recompile your application if the db-name changes

  10. #10
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString constants in a class

    Quote Originally Posted by jpn View Post
    Of course macros have their own use, but *cough* macros are evil! At least compilers and debuggers come along with typed variables. Try googling for "const vs #define".. http://www.comeaucomputing.com/techtalk/#definevsconst
    Funny position to take given that a lot of Qt goodness is due to macros and preprocessors

    Anyway, none of those reasons apply in this case, since the point is just to put the name in one place. I'm against heavy use of macros as well (I hate it when people define macros like MWPTR MainWindowFactor::getInterfacePointer()->mainWindow()). Those just serve to obfuscate code, but in this case a define is the easiest way to go.

  11. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QString constants in a class

    Quote Originally Posted by pherthyl View Post
    Funny position to take given that a lot of Qt goodness is due to macros and preprocessors
    But there's quite a big difference, don't you think? Most of the macros providing this goodness are actually defined as empty. They are just used as markers for moc.
    J-P Nurmi

  12. #12
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QString constants in a class

    Ah, that I didn't know... Thanks for the info.

  13. #13
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QString constants in a class

    Quote Originally Posted by pherthyl View Post
    I tend to just go:
    Qt Code:
    1. #include "myclass.h"
    2. #define DBNAME "MyDatabase"
    To copy to clipboard, switch view to plain text mode 

    Not very C++y, but it works and its simple. I don't see any reason to use a static variable instead.
    This is fine but static const version is even better because it avoids creation of temporary QString objects at numerous place. And also, the assignment of const strings are faster thanks to implicit sharing of QString.

    So according to me static const QString DBNAME("string") is better solution.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

Similar Threads

  1. Delayed Rendering of QTabWidget Tabs
    By mclark in forum Qt Tools
    Replies: 13
    Last Post: 14th May 2007, 22:53
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  3. How to pass a QString to another class ?
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 9th December 2006, 20:16
  4. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.