Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The best way to store const strings.
Forum Updated to NodeBB v4.3 + New Features

The best way to store const strings.

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • HarbH Offline
    HarbH Offline
    Harb
    wrote on last edited by Harb
    #1

    Hello! In my Qt project I want to store a number of constant strings. I am going to display that strings on the screeen, compare them with other strings, write them to the file etc. What is the best way of implementation? There are too many options, and I am a little bit confused, course lack of expirience. Should I use QString, standard C++ string class, or just char*? And where I should declare and define them? For example I can make some sort of "GLOBAL.H" file, and declare them there as extern const, or I can declare them inside my MainWindow class as static const.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Do you need unicode support?
      How will you need to access them when you compare ?
      By index or do you need to find a string first?

      The good old style would be
      global.h and extern const.
      Nothing wrong with that :)

      QString offer nice functions but it is also easy to construct a Qstring from char * when needed so you do not need to have them all as QStrings.

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        The format depends on usage pattern you plan. For example there's no point in storing std::strings if you want to pass them to Qt functions expecting QStrings, as you would waste CPU on conversions. Similarly there's no point in creating QStrings that you would for example send to a web service expecting ASCII. If you go with std::string or const char* figure out what encoding will you use and stick to it.
        Unless working with WinAPI or other 3rd party apis that requires them don't even consider std::wstring or const wchar_t*. They are a mess.
        Figure out what format you will need at runtime and stick to that. If possible stick to one format. String conversions are a performance killing plague.

        As for how to incorporate them into your program - again - it depends on the usage. How often will you need them. How fast access to the you need i.e. multiple times in tight loops or a just a couple of calls across whole execution. Remember that QString and std::string are classes with some overhead (small but still) while const char* is just a plain old pointer. How many are them? How long are they? Do they use non-ascii characters? What is their purpose - user facing translatable messages or some data IDs? Where are ou going to use them - in a single class or across whole app/modules? Depending on these and more factors you may consider anything from simple extern pattern you mentioned, a resource, loading from external data store (file or database), making them a string factory class members or function results etc.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved