我開始用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