Val*_*ria 2 qt qml qtquick2 qtquickcontrols2
目前我有一个以下列方式打开的窗口:
property variant win
Button {
id: testButton
MouseArea {
onClicked: {
var component = Qt.createComponent("test.qml");
win = component.createObject(testButton);
win.show();
}
}
}
Run Code Online (Sandbox Code Playgroud)
创建这样的窗口是否可以,或者有更好的方法来做到这一点(来自 QML,而不是来自 C++)?
当我关闭这个附加窗口时(只需单击“x”按钮),我想将它连接到另一个事件(例如,更改按钮的颜色)。怎么做?
谢谢。
让它更具声明性通常会更好。如果你希望你的按钮只打开一个窗口,a 的用法Loader可能适合你。
我认为这就是您想要的,因为您将其存储在一个变量中,如果多次单击该按钮,您将无法访问您的实例。如果您需要大量Windows相同的Button,您可以使用 aListModel和 aInstantiator来创建实例。
随着Loader这可能是这样的:
Button {
id: ldbutton
onClicked: winld.active = true
Rectangle {
id: ldindic
anchors {
left: parent.left
top: parent.top
bottom: parent.bottom
}
width: height
color: winld.active ? 'green' : 'red'
}
Loader {
id: winld
active: false
sourceComponent: Window {
width: 100
height: 100
color: 'green'
visible: true
onClosing: winld.active = false
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中也已经回答了你的第二个问题:你正在寻找的信号被称为closing- 连接到它来做任何必要的事情。
在Loader需要卸载窗口的情况下,也许可以稍后再加载。如果你有一个创建的窗口Instantiator,你需要从删除相应指标Instantiator的ListModel。
这可能如下所示:
Button {
id: rpbutton
onClicked: rpmodel.append({})
text: 'Open Windows ' + rpmodel.count
ListModel {
id: rpmodel
}
Instantiator { // from QtQml 2.0
model: rpmodel
delegate: Window {
width: 100
height: 100
color: 'blue'
visible: true
onClosing: rpmodel.remove(index)
}
}
}
Run Code Online (Sandbox Code Playgroud)
在您的代码中,您可以通过使用Connection连接到您的属性的-objectwin或通过onClicked像这样更改 JS来连接到它:
onClicked: {
var component = Qt.createComponent("test.qml");
win = component.createObject(testButton);
win.closing.connect(function() { console.log('do something') })
win.show();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4357 次 |
| 最近记录: |