打开和关闭其他窗口(QML) [英] Open and close additional window (QML)

查看:1154
本文介绍了打开和关闭其他窗口(QML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我可以通过以下方式打开一个窗口:

Currently I have a window openning in the following way:

property variant win
Button {
    id: testButton
    MouseArea {
        onClicked: {
            var component = Qt.createComponent("test.qml");
            win = component.createObject(testButton);
            win.show();
        }
    }
}

  1. 可以像这样创建一个窗口吗,或者有更好的方法(从QML,而不是从C ++)?

  1. Is it ok to create a window like this or there is a better way to do it (from QML, not from C++)?

当我关闭此附加窗口(仅通过单击"x"按钮)时,我想将其连接到另一个事件(例如,更改按钮的颜色).怎么做?

When I close this additional window (just by clicking "x" button), I want to connect it to another event (for example, changing color of the button). How to do it?

谢谢.

推荐答案

通常具有更好的声明性会更好.如果您希望按钮仅打开一个窗口,则Loader的用法可能很适合您.
我认为这就是您想要的,因为您将其存储在一个变量中,并且如果多次单击该按钮,将会失去对实例的访问权限.如果需要由同一Button创建的大量Windows,则可以使用ListModelInstantiator创建实例.

It is usually nicer to have it more declarative. If you want your button to only open one window, the usage of a Loader might be right for you.
I think this is what you want, as you store it in one variable, and if you click the button multiple times, you would lose access to your instance. If you need a larger amount of Windows created by the same Button, you might use a ListModel and a Instantiator to create the instances.

使用Loader可能看起来像这样:

With the Loader this might look like this:

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
        }
    }
}

在此代码中,您的第二个问题也已经答案:您正在寻找的信号称为

In this code is also already the answer to your second question: The signal you are looking for is called closing - connect to it to do what ever is necessary.

Loader的情况下,有必要卸载该窗口,以便以后可以再次加载.如果您具有由Instantiator创建的窗口,则需要从InstantiatorListModel中删除相应的索引.

In the case of the Loader it is necessary to unload the window, so it can be loaded again later, maybe. If you have the window created by a Instantiator, you need to remove the corresponding index from the Instantiator's 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)
        }
    }
}

在您的代码中,您可以通过使用连接到属性winConnection对象或通过像这样更改JS onClicked来连接到它:

In your code you could connect to it, either by using a Connection-object, that connects to your property win, or by changing the JS onClicked like so:

onClicked: {
    var component = Qt.createComponent("test.qml");
    win = component.createObject(testButton);
    win.closing.connect(function() { console.log('do something') })
    win.show();
}

这篇关于打开和关闭其他窗口(QML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆