1

I really don't know what's the problem. I know that i'm using the correct instance because I set this class as a context, and even better, the handler is invoked. I also pass the arguments to the c++ signal by value. what is the problem and how to solve it?

main.cpp

websocket_session sess;
rootContext->setContextProperty("websocketSession", &sess);
const QUrl url(QStringLiteral("qrc:/main.qml"));

main.qml

Connections {
    target: websocketSession;
    onLoginResponseProcessed: {
        console.log(logged_in, role)
    }
}

websocket_session.hpp

class websocket_session : public QObject
{
    Q_OBJECT

    QWebSocket websocket_;
    char *buffer_;

    QString url_;
    bool autoConnect_;
    bool rememberMe_;
    QString username_;
    QString password_;

public:
    explicit websocket_session(QObject *parent = nullptr);
    ~websocket_session();

    Q_INVOKABLE void send(const control_messages::Request &req);

    Q_INVOKABLE void init(const QString &url, const QString &username, const QString &password);

    void process_message(const std::string &data);

    //Messages

    Q_INVOKABLE void login(const QString &username, const QString &password);

private slots:
    void onConnected();
    void onDisconnected();
    void onTextMessageReceived(const QString &message);
    void onError();

signals:
    void loginResponseProcessed(bool logged_in, RoleWrapper::Role role);
    void error(const QString &error);
};

RoleWrapper.h

#ifndef ROLEWRAPPER_H
#define ROLEWRAPPER_H

#include <QObject>

namespace RoleWrapper
{
    Q_NAMESPACE
    enum Role {
        USER,
        ADMIN
    };
    Q_ENUM_NS(Role)
}

#endif // ROLEWRAPPER_H

I saw this thread that says it's a bug: Qml - c++ signal parameters "undefined" in qml

main.qml prints:

qml: undefined undefined

If the problem is indeed a bug then how can I overcome this problem?

UPDATE this is the code that emits the signal:

websocket_session.cpp

case LOGIN: {
            LoginResponse loginResponse;
            payload.UnpackTo(&loginResponse);
            auto logged_in = loginResponse.loggedin();
            auto role = static_cast<RoleWrapper::Role>(loginResponse.role());

            std::cout << "logged_in: " << logged_in << ", role: " << loginResponse.role() << role << Role_Name(loginResponse.role()) << std::endl;

            emit loginResponseProcessed(logged_in, role);
            break;
        }
guy
  • 177
  • 2
  • 14
  • there is no error message. the console.log(logged_in, role) in main.qml prints: qml: undefined undefined – guy Dec 30 '19 at 23:37
  • alright, but meanwhile I will add the RoleWrapper::Role code to the question.. – guy Dec 30 '19 at 23:40
  • Based on Qt 5.14.0 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit) – guy Dec 30 '19 at 23:50

1 Answers1

1

You have to register the type:

qRegisterMetaType<RoleWrapper::Role>();
eyllanesc
  • 235,170
  • 19
  • 170
  • 241