我开始用QML编写应用程序(使用QtQuick 1.1和Qt 4.8.1),我有几个关于信号的问题。在我的项目有以下文件:QML信号连接
main.qml:
Rectangle {
signal sigExit()
width: 800
height: 600
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
sigExit();
Qt.quit();
}
}
Button
{
x: 10
y: parent.height-height-5
text: "someText"
}
}
Button.qml:
Rectangle {
signal buttonsig()
width: 60
//(...)
MouseArea
{
anchors.fill: parent
onClicked: buttonsig();
}
}
当我想从信号连接main.qml
到C++插槽,我做的:
main.cpp:
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/MyProject/main.qml"));
viewer.showExpanded();
MyClass* obj = new MyClass;
QObject* item = qobject_cast<QObject*>(viewer.rootObject());
QObject::connect(item, SIGNAL(sigExit()), obj, SLOT(onExitWindow()));
它工作。但是当我想将sigbutton()
从Button.qml
连接到C++插槽时怎么办?它会是这样的?
QObject *rect = item->findChild<QObject*>("Button");
QObject::connect(rect, SIGNAL(buttonsig()), obj, SLOT(onExitWindow()));
而第二个问题:我如何连接到sigbutton()
main.qml
(例如,我想他们点击后,改变我的按钮的位置)?
谢谢!那么第二个问题呢?是否有可能将信号从一个QML文件连接到另一个QML文件中的插槽(或类似的东西)? – trivelt 2014-10-11 11:49:54