[英]QML handler is invoked but with "undefined" c++ signal parameters
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.
我还按值将参数传递给 c++ 信号。 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 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我看到这个线程说这是一个错误: Qml - c++ 信号参数在 qml 中“未定义”
main.qml prints: main.qml 打印:
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 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;
}
您必须注册类型:
qRegisterMetaType<RoleWrapper::Role>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.