【问题标题】:How dynamically creates Popup in QML如何在 QML 中动态创建 Popup
【发布时间】:2017-03-27 04:58:43
【问题描述】:

当我尝试使用 Qt.createQmlObject(...)Qt.createComponent(...) 动态创建 Popup 时,出现异常:

QML 弹出窗口:找不到任何可在其中打开弹出窗口的窗口。

这是我的代码:

var popup1 = Qt.createQmlObject('import QtQuick 2.8; import QtQuick.Controls 2.1; Popup { id: popup; x: 100; y: 100; width: 200; height: 300; modal: true; focus: true; closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent; visible: false }',
                                window,
                                "DynamicPopup");
popup1.open()

var popupComponent = Qt.createComponent("qrc:/TestPopup.qml")
var popup2 = popupComponent.createObject(window);
popup2.open()

TestPopup.qml:

import QtQuick.Window 2.2
import QtQuick.Controls 2.1

Popup {
    x: 100
    y: 100
    width: 200
    height: 300
    modal: true
    focus: true
    closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
    visible: false
}

【问题讨论】:

    标签: qt qml qt5 qtquick2


    【解决方案1】:

    Popup 没有继承QQuickItem,默认情况下它是QML Window 的父级,如果您使用QQuickWidget,则不会实例化。因此传递parent 应该如下完成:

    var popupComponent = Qt.createComponent("qrc:/TestPopup.qml")
    var popup2 = popupComponent.createObject(window, {"parent" : window});
    popup2.open()
    

    【讨论】:

    • @A.J,在你的例子中window 是什么?尝试将Popup绑定到QQuickItem
    • 好吧,我的窗口是一个ApplicationWindow。非常感谢。
    【解决方案2】:

    父元素必须是继承自QQuickItem的元素

    例子:

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.1
    
    Window {
        id: win
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Row{
            Button{
                id: item1
                text: "btn1"
                onClicked: {
                    var popup1 = Qt.createQmlObject('import QtQuick 2.8; import QtQuick.Controls 2.1; Popup { id: popup; x: 100; y: 100; width: 200; height: 300; modal: true; focus: true; closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent; visible: false }',
                                                    item1,
                                                    "DynamicPopup");
                    popup1.open()
    
                }
            }
    
            Button{
                id: item2
                text: "btn2"
                onClicked: {
                    var popupComponent = Qt.createComponent("qrc:/TestPopup.qml")
                    var popup2 = popupComponent.createObject(item2);
                    popup2.open()
                }
    
            }
    
        }
    }
    

    方法一:

    方法二:

    【讨论】:

      【解决方案3】:

      Popup 需要以 Item 为父对象,window 不是一个。 你应该改用window.contentItem

      【讨论】:

        【解决方案4】:

        使用 Loader 动态加载弹出窗口的好方法:

        Loader {
            id: popupLoader
        
            active: false
            source: "qrc:/TestPopup.qml"
            onLoaded: item.open()
        }
        
        function openMyPopup() {
            if( popupLoader.active )
                popupLoader.item.open()
            else
                popupLoader.active = true
        }
        

        【讨论】:

          猜你喜欢
          • 2018-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-15
          • 2021-10-24
          • 2017-12-10
          • 1970-01-01
          相关资源
          最近更新 更多