Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946
The best way to store const strings.
Harb last edited by Harb
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.
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.
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.