【发布时间】:2014-10-11 11:26:05
【问题描述】:
我开始使用 QML 编写应用程序(使用 QtQuick 1.1 和 Qt 4.8.1),我有几个关于信号的问题。在我的项目中有以下文件:
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(例如,我想在单击按钮后更改按钮的位置)?
【问题讨论】:
标签: c++ qt signals qml qt-quick