Results 1 to 2 of 2

Thread: Put all labels in an external file, which pattern to use ?

  1. #1
    Join Date
    Mar 2009
    Location
    Tunisia
    Posts
    38
    Thanks
    12
    Platforms
    Windows

    Default Put all labels in an external file, which pattern to use ?

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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Put all labels in an external file, which pattern to use ?

    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.
    Qt Code:
    1. int main(...) {
    2. QApplication app(argc, argv);
    3.  
    4. QCoreApplication::setOrganizationName("MySoft");
    5. QCoreApplication::setOrganizationDomain("mysoft.com");
    6. QCoreApplication::setApplicationName("Star Runner");
    7.  
    8. ...
    9. }
    10.  
    11. // elsewhere
    12. void foo() {
    13. QSettings settings;
    14. QString label = settings.value("Label", "Default value").toString();
    15. }
    16.  
    17. void bar() {
    18. QSettings settings;
    19. QString label = settings.value("OtherLabel", "Default value").toString();
    20. }
    To copy to clipboard, switch view to plain text mode 
    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.

Similar Threads

  1. Qt Designer Use external stylesheet file (qss)
    By florianwalter in forum Qt Tools
    Replies: 0
    Last Post: 27th May 2010, 11:57
  2. QML file not loading external JS file?
    By anothertest in forum Qt Programming
    Replies: 2
    Last Post: 14th April 2010, 09:11
  3. Open an external file from qt
    By maider in forum Qt Programming
    Replies: 9
    Last Post: 12th March 2010, 10:09
  4. Using qDebug to write to an external file
    By qtUser500 in forum Newbie
    Replies: 4
    Last Post: 18th November 2009, 14:01
  5. Database: How to use an external file?
    By goes2bob in forum Newbie
    Replies: 10
    Last Post: 23rd January 2008, 14:07

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.