使用 QMetaEnum 将枚举转换为字符串 [英] Converting enum into String using QMetaEnum

查看:84
本文介绍了使用 QMetaEnum 将枚举转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为这个主题搜索了很多,已经找到了一些方法,但我遇到了一些错误,我找不到原因.

I have searched a lot for this topic and already found some approach but I get some errors I can't find the reason of it.

想法是使用 QMetaEnum 从枚举中读取键,以便稍后将字符串填充到组合框中.

Idea is to read the keys from the enum with QMetaEnum to fill the strings in a combobox later.

我已经有了枚举,并且还在枚举所在的类中设置了 Q_Object 和 Q_Enum 宏.但是我得到了对'Planet:: metaObject() const'的未定义引用";使用 QMetaEnum 的错误消息.

I have already the enum and also setup Q_Object and Q_Enum Macro in the class where the enum is. But I am getting "undefined reference to 'Planet:: metaObject() const'" error message by using the QMetaEnum.

这里是行星.h

#include <QMetaEnum>
class Planet
{
    Q_OBJECT
public:    
    enum PlanetTypes
    {
       Barren,Gas,Ice,Lava,Oceanic,Plasma,Storm,Temperate
    };Q_ENUM(PlanetTypes)
    Planet();
    //some getters and setters for my private member-variables
};

这是我尝试读取枚举并获取错误消息的片段.

Here is the snipped where I try to read the enum and getting the error message.

QStringList DataModel::getPlanetTypes()
{
   QStringList PlanetTypesList;

   Planet p;
   const QMetaObject* metaObj = p.metaObject();
   QMetaEnum e = metaObj->enumerator(metaObj->indexOfEnumerator("PlanetTypes"));
   for(int i=0; i<e.keyCount();i++)
   {
        PlanetTypesList.append(e.key(i));
   }
   return PlanetTypesList;

}

错误指的是行:

QMetaEnum e = metaObj->enumerator(metaObj->indexOfEnumerator("PlanetTypes"));

我什至尝试从 QObject 继承行星但它也没有解决问题.

I even tried to inherit planet from QObject but it also did not solve the issue.

如果能得到一些帮助以及 QMetaEnum 用法的进一步解释,那就太好了.

Would be really cool to get some help and maybe some further explanation of QMetaEnum usage.

它也给我一个错误提示:未定义对vtable for Planet"的引用,如果这有助于以某种方式了解此问题的来源.

it also gives me an error saying: undefined reference to 'vtable for Planet' if that helps somehow to get an idea where this issue comes from.

我发现这是针对vtable"问题的,但它也无法解决此错误.Qt 未定义对 vtable 的引用

i've found this for the 'vtable' issue but it also does not resolve this error. Qt undefined reference to vtable

推荐答案

我找到了问题的答案.

当我研究这个 vtable 问题时,我发现了这篇文章.C++ - 对 `vtable 的未定义引用

As i was researching about this vtable issue i've found this post. C++ - Undefined reference to `vtable

我试了一下,从类中删除了 Q_Object 宏.然后两个错误都消失了

and i have given it a shot and removed Q_Object macro from the class. Then both errors disappear

这并不能解决问题!但您可以在这里找到解决方案:QMetaEnum 不从枚举中读取键 因为我做了第二篇文章没有意识到这两个问题相互关联.

This does not solve it! But you can find the solution here: QMetaEnum does not read keys from enum As i made the second post i wasn't aware that both issues were connected with each other.

这篇关于使用 QMetaEnum 将枚举转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆