简体   繁体   English

将Qt属性枚举值打印为键字符串

[英]Printing a Qt property enum value as a key string

I have a QML Loader and on some event I want to print its status property, but I'm getting 0 on the console, even if I use toString() . 我有一个QML Loader ,在某些情况下我想打印它的status属性,但是即使使用toString() ,我在控制台上也得到0 So I need to always refer to the order of the enum values in qquickloader_p.h to know what's going on. 因此,我需要始终参考qquickloader_p.h中的枚举值的顺序以了解发生了什么。

Any other way? 还有其他方法吗?

There is actually a way to get the enum key without knowing or even passing the enum name, if the following syntax suits your needs: 如果以下语法适合您的需要,实际上有一种方法可以获取枚举键,而无需知道甚至不传递枚举名称:

Info.print(loader, "status")

You can get it via this: 您可以通过以下方式获取它:

#include <QMetaType>
#include <QMetaProperty>
#include <QMetaEnum>
#include <QObject>
#include <QDebug>

class EnumInfo : public QObject {
    Q_OBJECT
  public slots:
    void print(QObject * obj, QString prop) {
      const QMetaObject * meta = obj->metaObject();
      int i = meta->indexOfProperty(qUtf8Printable(prop));
      if (i > -1) {
        QMetaProperty p = meta->property(i);
        if (p.isEnumType()) {
          QMetaEnum en = p.enumerator();
          qDebug() << en.name() << ":" << en.key(obj->property(p.name()).toInt());
        }
      }
    }
};

That object is best exposed to QML as a singleton, although for more casual use a context property won't be too much overhead. 该对象最好以单例形式暴露给QML,尽管对于更随意的使用,上下文属性不会带来太多开销。

The only other way would be to access that property from a C++ helper that looks up the enum's name using the metadata generated by moc. 唯一的其他方法是从C ++帮助器访问该属性,该帮助器使用moc生成的元数据查找枚举的名称。 See also this answer . 另请参阅此答案

ENUMs are used to make code more readable compared to the use of integers but faster than using strings for that purpose. 与使用整数相比,使用ENUM可使代码更具可读性,但比使用字符串更快。
Their intent is mainly comparison. 他们的意图主要是比较。

If you want to have the names to it, you need to create additional structures (eg arrays) to look them up. 如果要使用名称,则需要创建其他结构(例如数组)来查找它们。 Usually this is not necessary, so it is more efficient, not to do it. 通常,这不是必需的,因此效率更高,而不是这样做。

If you look at console.log(typeof(Loader.Ready)) you will find, that the ENUM is indeed nothing more than a number - and maybe some meta object information. 如果查看console.log(typeof(Loader.Ready))您会发现ENUM确实不过是一个number -也许还有一些元对象信息。

Due to the MetaObject, there is a way, even though uncomfortable... The thing is, you need to know the name of the ENUM 由于使用MetaObject,即使不舒服,也有一种方法...问题是,您需要知道ENUM的名称

enumspy.h enumspy.h

#ifndef ENUMSPY_H
#define ENUMSPY_H

#include <QObject>

class EnumSpy: public QObject
{
    Q_OBJECT
public:
    EnumSpy(QObject* parent = nullptr);

public slots:
    QString getEnumName(QObject* object, QString name, int index) const;
};

#endif // ENUMSPY_H

enumspy.cpp enumspy.cpp

#include <QMetaObject>
#include <QMetaEnum>
#include <QDebug>
#include "enumspy.h"

EnumSpy::EnumSpy(QObject* parent)
    : QObject(parent)
{

}

QString EnumSpy::getEnumName(QObject *object, QString name, int index) const
{
    const QMetaObject* meta = object->metaObject();
    QMetaEnum enm = meta->enumerator(meta->indexOfEnumerator(&name.toStdString()[0]));
    qDebug() << meta;
    return QString(enm.key(index));
}

Register it, create an object, and call the method. 注册它,创建一个对象,然后调用该方法。

Note: The name for the ENUMs of the Loader is Status myEnumSpy.getEnumName(myLoaderInstance, 'Status', 1) => "Ready" 注意:加载程序的ENUM的名称为Status myEnumSpy.getEnumName(myLoaderInstance, 'Status', 1) => "Ready"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM