首页 > 解决方案 > onctiveFocusChanged 上的动态创建和销毁对象

问题描述

我正在尝试动态创建元素(某些对象的属性),当焦点在某些特殊对象上发生变化时,然后当焦点在其他对象上发生变化时,应该从属性块中消失并创建新元素(如果获得焦点的对象有)

属性块

专注于元素时的动态创建

为此,我使用了来自 main.qml onActiveFocusChanged的​​信号,其中我发送 Item activeFocusItem作为参数

import QtQuick 2.0
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.15


 Frame {
    id: _propetries
    width: parent.width
    Layout.fillHeight: true
    Layout.margins: defaultMargin

    property Item propertyItem: null

    //! Creating function(slot) to display Propetries of object in focus
    function displayPropetries(elementName) {
        var reg = new RegExp("AgentTabButton_QMLTYPE*")
        var reg1 = new RegExp("Executor_QMLTYPE*")
        var newComponent;
        var sprite;
        if (reg.test(elementName) || reg1.test(elementName)) {
            newComponent = Qt.createComponent("PropetriesTemplate.qml");
            if (newComponent.status === Component.Ready) {
                sprite = newComponent.createObject(_layout);
                propertyItem = sprite;
            }
        }

    }

    function destroyingPropetries(elementName){
        if (propertyItem) {
            propertyItem.destroy();
        }
        displayPropetries(elementName);
    }

    Connections {
     target: _mainWindow
     onActiveFocusPropetriesElement : {
         destroyingPropetries(elementName)
     }

    }
        //! RowLayout using for future dynamic adding rows of propetries
        GridLayout {
            id: _layout
            columns: 1
            //rows: 1
            width: _propetries.width

            RowLayout {

                Label {
                    text: "Propetries"
                    font.pointSize: 12
                    Layout.margins: defaultMargin
                    }

                //! Button for closing propetries page
                ToolButton {
                    icon.source: "qrc:/resources/resources/Icons/Close_icon.png"
                    icon.height: defaultIconSize /2
                    icon.width: defaultIconSize /2
                    Layout.alignment: Qt.AlignRight
                    Layout.margins: defaultMargin

                    onClicked: {
                        _propetries.visible = false
                        }
                    }
                }
            }

}

管理创建和销毁的代码,还有那个模板:

import QtQuick 2.0
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.15

//! Defining template of propetries configuration
Item {

    //id: _innerItem
    z: 10
    width: parent.width

    //! Defining property for future usage
    property string titleName: "Title: "

    GridLayout {

        columns: 1
        rows: 1
        width: parent.width

        Label {
            id: _label
            text: titleName
            font.pointSize: 12
        }

        BorderedTextBox {
            id: _borderedTextBox
            Layout.alignment: Qt.AlignTop
        }
    }
}
 

它工作正常,但是当首先将焦点更改在具有属性并显示它们的对象上,然后将焦点更改在也具有属性的对象上然后 textInput 变大到焦点将更改为没有属性的项目和属性块清理的那一刻时,我遇到了麻烦.

标签: qtqmlqquickitem

解决方案


推荐阅读