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;
}