Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

javascript - C ++,Qt 5.12.5:Java脚本无法将Q_ENUM读取为字符串(C++, Qt 5.12.5: Q_ENUM not readable as string by Javascript)

With Qt 5.6.3 it was working, upgrading to Qt 5.12.5 it stopped.

(使用Qt 5.6.3可以正常工作,然后停止升级到Qt 5.12.5 。)

Javascript gets now 0 (int value of enum LogoutResult -> Success) and not 'Success' (a string).

(Javascript现在得到0(枚举LogoutResult的int值-> Success),而不是'Success'(字符串)。)

Our class with the enum:

(我们的类带有枚举:)

namespace Modules
{
namespace Utils
{
class LogoutChecker : public QObject
{
    Q_OBJECT

public:
    enum class LogoutResult
    {
        Success,
        Installing,
        Uninstalling,
        GameRunning,
        Ignoring
    };
    Q_ENUM(LogoutResult)

    LogoutChecker(QObject* parent = nullptr);
    ~LogoutChecker() {}

    LogoutResult getLogoutRequestResult();

private:
    LogoutResult getResult(CheckType type);
};
}
}

Where registered:

(在哪里注册:)

View::View(QObject* parent)
{
    qRegisterMetaType<Modules::Utils::LogoutChecker::LogoutResult>("LogoutResult");
    ...
}

Where used, called by Javascript:

(使用时,由Javascript调用:)

LogoutChecker::LogoutResult View::logout()
{
    const auto logoutResult = LogoutChecker().getLogoutRequestResult();

    if (logoutResult == LogoutChecker::LogoutResult::Success)
    {
        ...
    }

    return logoutResult;
}

If I return it converted to string:

(如果我将其转换为字符串:)

    return QVariant::fromValue(logoutResult).toString()

of course it works, but this should not be needed.

(当然可以,但是不需要。)

I've tried all I could think of (moving the qRegisterMetaType in other places, adding Q_DECLARE_METATYPE , etc) but no way to solve it.

(我已经尝试了所有我能想到的(在其他地方移动qRegisterMetaType ,添加Q_DECLARE_METATYPE等),但是没有办法解决。)

Any help would be appreciated.

(任何帮助,将不胜感激。)

  ask by marco translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...