至C+的QML信号+;插槽qt4.8 我有一个QT快速项目,我试图从QML中发射一个信号,并连接到C++中的一个时隙。p>
QML文件至C+的QML信号+;插槽qt4.8 我有一个QT快速项目,我试图从QML中发射一个信号,并连接到C++中的一个时隙。p>,qt4,signals-slots,Qt4,Signals Slots,QML文件 import QtQuick 1.0 //import com.net 1.0 Column { id: mainCol x: 20; y: 80 width: 320 //height: 62 opacity: 0 //NetCom{ id: netcom} Rectangle{ signal tcpMsg(string address, int port) objectName: "topRec" id: topRec width: 40;
import QtQuick 1.0
//import com.net 1.0
Column {
id: mainCol
x: 20; y: 80
width: 320
//height: 62
opacity: 0
//NetCom{ id: netcom}
Rectangle{
signal tcpMsg(string address, int port)
objectName: "topRec"
id: topRec
width: 40; height: 40
color: "blue"
Text{
text: "send tcp msg"; font.pointSize: 14
anchors.verticalCenter: topRec.verticalCenter
anchors.left: topRec.right
anchors.rightMargin: 10
}
Text{
id: statusTxt
//text: cppInterface.qml_text ; font.pointSize: 12
text: "status: "; font.pointSize: 12
anchors.horizontalCenter: topRec.horizontalCenter
anchors.top: topRec.bottom
anchors.bottomMargin: 10
}
MouseArea {
id: mouseArea
transformOrigin: Item.Center
anchors.fill: parent //anchor all sides of the mouse area to the rectangle's anchors
onClicked: {
topRec.tcpMsg("127.0.0.1", 8888)
console.log("clicked")
}
}
}
states:[
State {
name: "visible";
PropertyChanges { target:mainCol; opacity: 1;}
}
]
transitions:[
Transition {
from:""; to: "visible"; reversible: true
NumberAnimation { properties: "opacity"; duration: 500; easing.type: Easing.InOutQuad }
}
]
}
C++文件
#include <QtGui/QApplication>
#include <QtDeclarative>
#include "qmlcppmediator.h"
#include "qmlapplicationviewer.h"
#include "kernelcom.h"
#include "netcom.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
//needed to use C++ code in QML
qmlRegisterType<KernelCom>("com.kernel", 1, 0, "KernelCom");
qmlRegisterType<NetCom>("com.net", 1, 0, "NetCom");
QmlApplicationViewer viewer;
viewer.setWindowFlags(Qt::FramelessWindowHint);
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/testMenu/main.qml"));
//make signal from Qml file trigger slots in C++
QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("qml/testMenu/TCPmenu.qml"));
QObject *object = view.rootObject();
QObject *child = object->findChild<QObject*>("topRec");
qDebug()<<child;
NetCom client;
qDebug()<<QObject::connect(child,SIGNAL(tcpMsg(QString, quint16)), &client, SLOT(start(QString, quint16)));
viewer.showExpanded();
return app->exec();
}
我尝试过使用QdeCalativeEngine,将信号放在主qml项中,这样我就不必使用get child,我会一直收到上面的消息
谢谢刚刚弄明白,它无法连接,因为我在中使用的是qunit16数据类型,而不是int 我把线路改成了这个
connect(child,SIGNAL(tcpMsg(QString, int)), &client, SLOT(start(QString, int)))
然后它连接起来了
此外,我不需要QDeclarativeView,我已从QmlApplicationViewer设置了rootObject 刚刚发现,它无法连接,因为我在中使用的是qunit16数据类型,而不是int 我把线路改成了这个
connect(child,SIGNAL(tcpMsg(QString, int)), &client, SLOT(start(QString, int)))
然后它连接起来了
此外,我不需要QDeclarativeView,我已从QmlApplicationViewer设置了rootObject