简体   繁体   English

使用 createObject (null) 创建一个新的顶级 QML 窗口

[英]Creating a new top-level QML Window using createObject (null)

I am working on a desktop application where I need the user to be able to "undock" certain elements of the GUI.我正在开发一个桌面应用程序,我需要用户能够在其中“取消停靠”GUI 的某些元素。 I am currently achieving this by reparenting the element that needs to be undocked to the contentItem of a dynamically created ApplicationWindow.我目前正在通过将需要取消停靠的元素重新设置为动态创建的 ApplicationWindow 的 contentItem 来实现这一点。 This works well but only if I pass null or undefined as the parent to the createObject() function like so:这很有效,但前提是我将 null 或 undefined 作为父级传递给 createObject() 函数,如下所示:

var window_component = Qt.createComponent("Secondary_Window.qml")
var window_object = window_component.createObject(null, {section_name: section})

If I pass anything else to the createObject() function, like say the main window element or a child of the main window element, I get a window but I can not bring the main window in front of the new window by clicking on it.如果我将其他任何内容传递给 createObject() 函数,例如主窗口元素或主窗口元素的子元素,我会得到一个窗口,但无法通过单击将主窗口置于新窗口的前面。 Nor are there tabs that appear in the Windows taskbar indicating that the application is currently using multiple Windows. Windows 任务栏中也没有显示应用程序当前正在使用多个 Windows 的选项卡。 The new window literally remains up front at all times until it is closed.新窗口始终保持在前面,直到关闭为止。 So my question is: Is it good practice to dynamically create a new window in QML by calling window_component.createObject (null) or is there another way to achieve what I described above (by say passing a flag to the window)?所以我的问题是:通过调用 window_component.createObject (null) 在 QML 中动态创建一个新窗口是一种好习惯,还是有另一种方法来实现我上面描述的内容(比如向窗口传递一个标志)? I have been experimenting with different flag combinations but haven't yet found a combination that works.我一直在尝试不同的标志组合,但还没有找到有效的组合。 Also, the documentation of createObject() says that if you don't pass a parent as a parameter then the object will not be visible but that doesn't seem to be the case here.此外, createObject() 的文档说,如果您不将父对象作为参数传递,则该对象将不可见,但此处似乎并非如此。 The window is displaying just fine.窗口显示正常。

A parent is a defined concept for Item s. parentItem的定义概念。

A Window is not an Item so specifying null is not only ok but appropriate. Window不是Item因此指定null不仅可以而且合适。

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

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