Results 1 to 7 of 7

Thread: Config files best practices

  1. #1
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Config files best practices

    I have been having a debate with my co-worker about the best way to manage config files for multiple environments (mainly production vs. development) and was looking for some outside opinions since he is mainly a web developer and I am a less experienced programmer. The overall problem as stated is this:
    We have several small, in-house Qt applications with hardcoded configuration variables (database credentials, file paths, etc.) that I am planning to extract to XML files that the applications will read. Apart from it generally just being good practice not to hardcode these things, I would like to make for easy switching between development/testing and production settings, or any other environment we may need in the future.

    My co-worker's thought: A separate dummy config file for each environment (e.g., config.xml.production, config.xml.dev) and it is up to the developer to change the name of the file they want to config.xml, which is the file that the application looks for. I don't like this because I think it invites more user error and I don't want to have to deal with renaming files all the time.

    My thought: Either have one config.xml file with different nodes for different environments or a different config file for each environment, and then have a global variable in the application that decides which node/file to use (to be set by the developer as needed). My co-worker doesn't like this because he thinks that if we want to add more different environments it will be a hassle and that the config file selection shouldn't be within the application itself.

    I can see both sides of the argument, what I'm wondering is: Is there a best practice on this for Qt applications in particular? Are we both totally way off base in our approach and why? I'm open to any and all thoughts. There is far more collective experience on this forum than there is between my co-worker and myself.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Config files best practices

    What about passing the config file to use as a command line argument/parameter, i.e. --config=prod.xml or -p (for prod) -d (for dev), etc.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. The following user says thank you to jefftee for this useful post:

    ce_nort (31st August 2016)

  4. #3
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Config files best practices

    That wouldn't work so well for us as the applications are pretty much never called via the command line. The users of the applications are general retail employees so they click on the .exe file to run it, and when in development I build and run them in Qt Creator.

  5. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Config files best practices

    I was thinking more along the lines of desktop icons that passed the appropriate command line arguments, i.e. "Foo Prod", "Foo Develop", etc. but if that doesn't work for you, then obviously it's not a good choice...

    Good luck!
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Config files best practices

    Perhaps you could use a QSettings INI-type file to store your environment flag. The app looks for the file (and flag entry) at startup, and if it finds it, it uses that mode. Otherwise, it goes to the default mode (production, presumably). You'll only need the QSettings file on your development / testing machine, not on the machines deployed to users.

    By the way, a year or two ago I posted code here that provides a QSettings interface to XML files. You might find it easier to use for your user-side XML config files.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. The following user says thank you to d_stranz for this useful post:

    ce_nort (31st August 2016)

  8. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Config files best practices

    Quote Originally Posted by ce_nort View Post
    That wouldn't work so well for us as the applications are pretty much never called via the command line. The users of the applications are general retail employees so they click on the .exe file to run it, and when in development I build and run them in Qt Creator.
    That wouldn't be a problem as far as I can tell.

    If the application is started without arguments, it just uses the default path or default file name.

    If you run in QtCreator you can easily pass arguments as part of the run settings.

    Another option is to use an environment variable or even have both options, one overriding the other if both are present.

    Cheers,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    ce_nort (31st August 2016)

  10. #7
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Config files best practices

    Sounds like there are a few good options out there! Thanks for the ideas. I'll look into them and decide what will work best for us, they all sound like a potential improvement upon the approaches that we came up with.

Similar Threads

  1. What are best practices for QSS files?
    By danielvianna in forum Newbie
    Replies: 0
    Last Post: 25th September 2015, 17:26
  2. Make Config files unreadable
    By mrandreas in forum Qt Programming
    Replies: 2
    Last Post: 15th October 2011, 00:53
  3. Replies: 2
    Last Post: 9th October 2010, 05:34
  4. Creating pkg-config files
    By zarq in forum Installation and Deployment
    Replies: 0
    Last Post: 6th May 2010, 11:53
  5. Best practices concerning QStandardItemModel
    By tim47 in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2010, 05:29

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.