qt - 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 变大到焦点将更改为没有属性的项目和属性块清理的那一刻时,我遇到了麻烦.
解决方案
推荐阅读
- docker - Nginx 反向 SSL 代理 docker-compose
- api - 来自新闻 API 的 Vue CLI 可点击动态 URL
- java - Java在插入数据库之前验证列长度
- python - 用python创建windows快捷方式到“程序文件”
- c++ - Azure 存储 CPP 和 VCPKG
- regex - 用 sed 在括号前加逗号
- python - 从 json 中获取特定数据并存储在 django 的表中
- swift - SwiftUI 中的 FetchRequest 包装值:EXC_BAD_INSTRUCTION
- http-headers - WSO2 序列:捕获传入请求的标头
- java - 给通过Spring Logback生成的java应用程序日志文件754个linux文件权限