简体   繁体   English

QML 动态负载 Window 按下按钮,但如果存在则改为焦点

[英]QML Dynamic Load Window On Button Press, But Focus Instead If Exists

I have a button within my main ApplicationWindow ( root ) that dynamically loads and opens a second, different ApplicationWindow that is declared in a separate .qml file.我的主 ApplicationWindow ( root ) 中有一个按钮,可以动态加载并打开另一个不同的 ApplicationWindow,该按钮在单独的.qml文件中声明。

Button {
    id: btnLogger
    text: "Logger"
    onClicked: {
        var component = Qt.createComponent("logger.qml")
        var window    = component.createObject(logRoot)
        window.show()
    }
}

This works fine for opening a window when clicking the button.这适用于在单击按钮时打开 window。 Subsequent clicks create further new windows.随后的点击会进一步创建新的 windows。

My intent is that subsequent clicks should instead focus the preexisting window.我的意图是后续的点击应该关注先前存在的 window。 If the new window is later closed, then clicking the button should revert back to opening the window.如果新的 window 稍后关闭,则单击该按钮应恢复为打开 window。

ie if a window doesn't currently exist or exists but has been closed, create it and open it;即如果 window 当前不存在或存在但已关闭,则创建并打开它; else, focus it.否则,集中注意力。

How would this be done from within qml ?这将如何从qml内部完成? Alternatively, I am currently loading the application from a QQmlApplicationEngine in my C++ , how could I use that to achieve this functionality?或者,我目前正在从我的C++中的 QQmlApplicationEngine 加载应用程序,我该如何使用它来实现此功能?

The example code for my comment above:我上面评论的示例代码:

Button {
    id: btnLogger
    text: "Logger"
    property var wnd: undefined
    onClicked: {
        if(wnd == undefined)
        {
            var component = Qt.createComponent("logger.qml")
            wnd = component.createObject(logRoot);
            wnd.closing.connect(function() { btnLogger.wnd = undefined;});
            wnd.show();
        }
        else
        {
            wnd.requestActivate();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM