【发布时间】:2016-08-06 05:08:49
【问题描述】:
我有 2 个 qml 文件。 Toolbarbutton.qml 创建一个按钮,DockWidget.qml 创建一个 ListView。我试图让 Toolbarbutton 中的按钮向 DockWidget 广播该按钮已被单击。然后我想将项目添加到我的 listView 中。
我一直在尝试使用信号来建立通信。在 Toolbarbutton.qml 中,我有一个 ID 为 saveButton 的 Rectangle 项。在这个 Rectangle Item 下,我添加了一个名为 addSaveHistory() 的信号。我已经缩短了代码,以便更容易看到我在做什么。
Rectangle {
id: saveButton
width: 50
height: 30
border.color: "white"
color: buttonMouseArea.containsMouse ? "grey" : "black"
//Button text
Text{
id: buttonLabel
anchors.centerIn: parent
text: "Save +"
color: "white"
}
signal addSaveHistory(string url)
MouseArea{
id: buttonMouseArea
anchors.fill: parent //anchor the mousearea to the rect area
//onClicked handles valid mouse button clicks
onClicked: {
addSaveHistory("Hello?") //dispatch the event
}
}
}
在 DockWidget.qml 文件中,我有一个使用 Connections 组件的项目。我已经缩短了代码,因此更容易看到我在做什么。
Item{
width: 100
height: 100
objectName: "Save + History"
Rectangle {
id: rect
anchors.fill: parent
color: "#323232"
Connections {
target: saveButton //id of rectangle in ToolbarButton.qml
onAddSaveHistory: {
// this is never called
}
}
}
我似乎无法弄清楚为什么永远不会调用 onAddSaveHistory。我也尝试过使用 Loader 组件将 Dockwidget.qml 文件加载到 Toolbarbutton.qml 中,然后专门调用 Dockwidget 中的函数,但这也不起作用。
我对信号应该如何工作有错误的想法吗?我将不胜感激任何指导。
这是我的 main.qml 文件。
import QtQuick 2.2
import Painter 1.0
import "save.js" as Save
Plugin {
//executed at startup
Component.onCompleted:{
//add toolbar
alg.ui.addToolBarWidget("ToolbarButton.qml")//add a button to toolbar
alg.ui.addDockWidget("DockWidget.qml")//add dock widget
Save.log("Incremental Save Plugin has been created")
}
}
【问题讨论】:
标签: qt qml signals-slots