C++, Qt 5.12.5: Q_ENUM not readable as string by Javascript

142 Views Asked by At

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

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

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:

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.

Any help would be appreciated.

Added the JS part:

  const requestSignOut = () =>
    new Promise((resolve, reject) =>
      __app.logout(result => {
        if (result === 'Success') {
          resolve(result)
        } else {
          reject(result)
        }
      })
    )
0

There are 0 best solutions below