我想问简单的问题以获得快速解决方案。我在 main.qml 中有 ListView
和 Button
,在 openingtaskdelage.qml 中有 Component
。这是文件main.qml:
ListView
{
id: grid
delegate:mydelegate
model: mymodel
spacing: 5
orientation:ListView.Vertical
Layout.preferredWidth: width_commmon
Layout.preferredHeight: height_body
Layout.maximumHeight: 800
Layout.alignment: Qt.AlignHCenter
// highlightFollowsCurrentItem: false
focus: true
ClosingTaskModel
{
id:mymodel
}
ClosingTaskDelegate
{
id:mydelegate
}
}
Button
{
id:finishbutton
Layout.alignment: Qt.AlignHCenter
Layout.preferredWidth: width_commmon
Layout.preferredHeight:height_endbutton
text: "İş Emrini Tamamla"
onClicked:
{
// console.log((grid.children["border1"]).id);
}
}
和文件ClosingTaskDelegate.qml:
Component
{
id:component1
Rectangle
{
signal sendMenuValuestoJS
height: 200
width: 200
Text
{
id:text1;
text:header;
height:parent.height/2
// width: parent.width
anchors.horizontalCenter:parent.horizontalCenter;
font.pointSize:20;
color:"red";
// height:parent.height/2
// width: parent.width
}
}
}
不要在意细节。我的主要问题是:当 main.qml 中的 onClickButton()
发生时,必须触发 signal sendMenuValuestoJS()
。该信号必须发送字符串值作为参数(即发送文本)。
我无法从 main.qml 访问信号。我已在 Component
中定义了 signal,但出现以下运行时 QML 错误:
Component objects cannot declare new signals.
如何将信号连接到某些功能?
最佳答案
要使用什么Connections
?
这是这个想法,
/* main.qml */
Rectangle {
width: 200
height: 200
MouseArea {
id: mouse
anchors.fill: parent
}
Click {
id: click
targetto: mouse
}
}
/* Click.qml */
Item {
property variant targetto
Connections {
target: targetto
onClicked: console.log("CLICKED!");
}
}
关于qt - QML,如何从另一个 qml 访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34180748/