如何在 C++ Qt 中填充静态 QMap 的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8157625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:05:44  来源:igfitidea点击:

How do I populate values of a static QMap in C++ Qt?

c++qtqt4

提问by Di Zou

I have this in my C++ header file:

我的 C++ 头文件中有这个:

#include <QMap>
#include <QString>

class LogEvent {

public:
    LogEvent();

    enum column_t {TIMESTAMP_COLUMN = 0, TYPE_COLUMN = 1, EVENT_COLUMN = 2,
        FILE_COLUMN = 3};
    static QMap<column_t, QString> COLUMN_NAMES;

    static QMap<column_t, QString> getColumnNames() {
        return LogEvent::COLUMN_NAMES;
    }

    //blah blah blah other functions
};

This is my C++ source file:

这是我的 C++ 源文件:

#include "LogEvent.h"

LogEvent::LogEvent()
{
    //constructor code
}

//blah blah blah other functions

I want to add values to my static QMap COLUMN_NAMES. Where and how would I do this?

我想向我的静态 QMap COLUMN_NAMES 添加值。我将在哪里以及如何做到这一点?

采纳答案by userr1728854

You can use static function that returns initialized map:

您可以使用返回初始化映射的静态函数:

// source file:
QMap<column_t, QString> LogEvent::initColumnNames() {
    QMap<column_t, QString> map;
    map.insert(LogEvent::TIMESTAMP_COLUMN,"Timestamp");
    // and so on
    return map;
}

QMap<column_t, QString> LogEvent::COLUMN_NAMES = initColumnNames();

Also, in case you want to use strings internationalization in static variables and need to call QTextCodec::setCodecForTrfirst, it is a good idea to move static variable into its getter function:

此外,如果您想在静态变量中使用字符串国际化并且需要先调用QTextCodec::setCodecForTr,最好将静态变量移动到其 getter 函数中:

class LogEvent {

public:
// ...
    static QMap<column_t, QString> initColumnNames();

    static QMap<column_t, QString> getColumnNames() {
        static QMap<column_t, QString> COLUMN_NAMES = initColumnNames();
        return COLUMN_NAMES;
    }
}

This way COLUMN_NAMESwill be initialized with the first call of getColumnNames()and you can set the text codec beforehand. But this is not thread-safe.

这种方式COLUMN_NAMES将在第一次调用时初始化,getColumnNames()您可以预先设置文本编解码器。但这不是线程安全的。

回答by Luchs

In the meantime, Qt 5.2 added support for C++11 initializer lists in QMap:

同时,Qt 5.2 在 QMap 中添加了对 C++11 初始值设定项列表的支持:

QMap::?QMap(std::initializer_list<std::pair<Key, T> > list)

This means you can initialize your map like this:

这意味着您可以像这样初始化您的地图:

static QMap<QString, int> my_map{{"a", 1}, {"b", 2}, {"c", 3}};

回答by Al Conrad

Another approach using an initializer list.

另一种使用初始化列表的方法。

QMap<QString, QString> qmap_(std::map<QString, QString> {{ "", "" }});

This works for older Qt before Qt was fixed to work with initializer lists directly.

在 Qt 被修复为直接使用初始化列表之前,这适用于较旧的 Qt。

回答by fghj

You can create std::map and use it to init QMap, or just replace QMap with std::map:

您可以创建 std::map 并使用它来初始化 QMap,或者只是用 std::map 替换 QMap:

 static const std::pair<T1, T2> arr[] = { 
     std::pair<T1, T2>(v1, v2),
     std::pair<T1, T2>(v3, v4),
};

static std::map<T1, T2> my_map(arr, arr + sizeof(arr) / sizeof(arr[0]));
QMap<column_t, QString> LogEvent::COLUMN_NAMES(my_map);

Another way is init COLUMN_NAMES in constructor:

另一种方法是在构造函数中初始化 COLUMN_NAMES:

LogEvent::LogEvent() {
   if (COLUMN_NAMES.isEmpty())
     ;//init it here

but this is not thread safe, the first one method was called before main, while the second during may be called during runtime.

但这不是线程安全的,第一个方法在 main 之前调用,而第二个方法可能在运行时调用。