开发者

qt "resource" string

开发者 https://www.devze.com 2023-03-11 01:13 出处:网络
I am wanting to have a pla开发者_如何转开发ce where i can store all the strings used in my applicaton, so i can modify them in one place and not all the places. Something like a resource file, where i

I am wanting to have a pla开发者_如何转开发ce where i can store all the strings used in my applicaton, so i can modify them in one place and not all the places. Something like a resource file, where i can put a label on the strings and just call the label.

I am not aware of anything offered by QT for this, so would I just need to create a header file with all those strings and include it everywhere I need it? What is the appropriate way to do this and could you offer a small example?

Thanks!!


I haven't used it yet, but I think, that the Qt Internationalization would allow you to do something like this, since one of it's options is to take all strings out of the application code so they can be replaced by translations. Even if you don't want to use any other features of this module, it would allow you to solve your problem. Replacing a string for a label would look like this:

QLabel *label = new QLabel(tr("Password:"));

The tr() function is already part of the Qt classes and you get a few more functions and macros for free that help to search and replace strings. The strings to be replaced can then be managed with QtLinguist. You can find a more detailed explanation here: Internationalization with Qt


In the old days[1], when using Windows resources, people have been using:

// in your project_strings.h file
#define STRING_PASSWORD 1
...

// resources project.rc
#include "project_strings.h"
STRINGTABLE
BEGIN
STRING_PASSWORD "Password:"
...
END

// in some other file
#include "project_strings.h"
CString str(STRING_PASSWORD);

The CString knew about windows resources (ugly dependency) and could go and read the string password. The #define is definitively very ugly in modern C++, but resources would not understand a static const variable or an inline function.

The easiest way to replicate this in a somewhat similar way is to use a header file with string declarations and then reference those strings anywhere you need them.

// in your project_strings.h
namespace MyProjectStrings {
const char *password;
...
}

// the project_strings.cpp for the strings
#include "project_strings.h"
namespace MyProjectStrings {
const char *password = "Password:";
...
}

// some random user who needs that string
#include "project_strings.h"
std::string password(MyProjectStrings::password);

Now all your strings are in project_strings.cpp and you cannot as easily translate them with tr()... but you could transform all those strings declarations with functions:

// in your project_strings.h
namespace MyProjectStrings {
const char *password(); //[2]
...
}

// the project_strings.cpp for the strings
#include "project_strings.h"
namespace MyProjectStrings {
const char *password() { return QObject::tr("Password:"); }
...
}

// some random user who needs that string
#include "project_strings.h"
std::string password(MyProjectStrings::password()); //[3]

And Voilà! You have a single long table of all your strings in one place and translatable.

[1] Many people still use that scheme!

[2] The function could return std::string to 100% prevent modifying the original.

[3] In this last example the string reference uses () since it's a function call.

0

精彩评论

暂无评论...
验证码 换一张
取 消