我正在尝试为我的项目为QML创建一个自定义TCP小部件(WebSocket发送HTTP请求,它不允许我发送原始TCP数据包)。我创建了“TCPSocketConnection”(在cpp中命名为TCPSocketConn)类,并使用占位符方法为其提供了一些属性,并尝试将其插入到QML中。当我尝试使用该类时,我收到以下错误
qrc:/qml/qmlwebsocketclient/main.qml:43:9: Cannot assign to non-existent property "onTextMessageReceived"
我有一个像这样的QML文件
****************************************************************************/
import QtQuick 2.0
import QtWebSockets 1.0
import Qt.Comm 2.0
Rectangle {
width: 360
height: 360
TCPSocketConnection{
id: socket
url: "ff"
onTextMessageReceived: {
console.log("On Recieve: " + messsage)
}
}
}
TCPSocketConnection定义如下
qmlRegisterType<TCPSocketConn>("Qt.Comm", 2, 0, "TCPSocketConnection");
在tcpsocketconn.h
#ifndef TCPSOCKETCONN_H
#define TCPSOCKETCONN_H
#include <QQuickItem>
class TCPSocketConn : public QQuickItem
{
Q_PROPERTY(QString url READ url WRITE a_1 NOTIFY a_2)
Q_PROPERTY(QString message READ message WRITE a_3 NOTIFY textMessageRecieved)
Q_PROPERTY(int status READ status WRITE a_4 NOTIFY statusChanged)
public:
explicit TCPSocketConn(QQuickItem *parent = 0);
QString url();
QString message();
int status();
signals:
void a_2();
void textMessageReceived();
void statusChanged();
public slots:
void a_1(QString);
void a_3(QString);
void a_4(int);
};
#endif // TCPSOCKETCONN_H
使用占位符cpp文件
#include "tcpsocketconn.h"
TCPSocketConn::TCPSocketConn(QQuickItem *parent) : QQuickItem(parent)
{
}
QString TCPSocketConn::url(){
return "";
}
QString TCPSocketConn::message(){
return "";
}
int TCPSocketConn::status(){
return 0;
}
void TCPSocketConn::a_1(QString d){
}
void TCPSocketConn::a_3(QString d){
}
void TCPSocketConn::a_4(int s){
}
void TCPSocketConn::textMessageReceived(){
}
void TCPSocketConn::statusChanged(){
}
void TCPSocketConn::a_2(){
}