Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 嵌套矩形_Python_Pyqt_Qml_Pyqt5 - Fatal编程技术网

Python 嵌套矩形

Python 嵌套矩形,python,pyqt,qml,pyqt5,Python,Pyqt,Qml,Pyqt5,目标: 我有一个XML文件,其中包含嵌套的矩形作为数据。每个矩形都有x和y坐标以及宽度和高度。这些嵌套矩形的深度未知,可以是1,也可以是XML嵌套元素的限制。在下面的示例中,深度仅为4,但实际数据未知 <?xml version="1.0" encoding="UTF-8"?> <rect x="0" y="0" width="600" height="200" name="scan"> <rect name="keyboard" x="0" y="50"

目标:

我有一个XML文件,其中包含嵌套的矩形作为数据。每个矩形都有x和y坐标以及宽度和高度。这些嵌套矩形的深度未知,可以是1,也可以是XML嵌套元素的限制。在下面的示例中,深度仅为4,但实际数据未知

<?xml version="1.0" encoding="UTF-8"?>
<rect x="0" y="0" width="600" height="200" name="scan">
    <rect name="keyboard" x="0" y="50" width="450" height="150" >
        <rect x="0" y="50" width="150" height="50" name="eta">
            <rect x="0" y="0" width="50" height="50" name="e">
            </rect>
            <rect x="50" y="0" width="50" height="50" name="t">
            </rect>
            <rect x="100" y="0" width="50" height="50" name="a">
            </rect>
        </rect>
        <rect x="150" y="50" width="150" height="50" name="oin">
            <rect x="0" y="0" width="50" height="50" name="o">
            </rect>
            <rect x="50" y="0" width="50" height="50" name="i">
            </rect>
            <rect x="100" y="0" width="50" height="50" name="n">
            </rect>
        </rect>
        <rect x="300" y="50" width="150" height="50" name="shr">
            <rect x="0" y="0" width="50" height="50" name="s">
            </rect>
            <rect x="50" y="0" width="50" height="50" name="h">
            </rect>
            <rect x="100" y="0" width="50" height="50" name="r">
            </rect>
        </rect>
        <rect x="0" y="100" width="150" height="50" name="dlc">
            <rect x="0" y="0" width="50" height="50" name="d">
            </rect>
            <rect x="50" y="0" width="50" height="50" name="l">
            </rect>
            <rect x="100" y="0" width="50" height="50" name="c">
            </rect>
        </rect>
        <rect x="150" y="100" width="150" height="50" name="umw">
            <rect x="0" y="0" width="50" height="50" name="u">
            </rect>
            <rect x="50" y="0" width="50" height="50" name="m">
            </rect>
            <rect x="100" y="0" width="50" height="50" name="w">
            </rect>
        </rect>
        <rect x="300" y="100" width="150" height="50" name="fgy">
            <rect x="0" y="0" width="50" height="50" name="f">
            </rect>
            <rect x="50" y="0" width="50" height="50" name="g">
            </rect>
            <rect x="100" y="0" width="50" height="50" name="y">
            </rect>
        </rect>
        <rect x="0" y="150" width="150" height="50" name="pbv">
            <rect x="0" y="0" width="50" height="50" name="p">
            </rect>
            <rect x="50" y="0" width="50" height="50" name="b">
            </rect>
            <rect x="100" y="0" width="50" height="50" name="v">
            </rect>
        </rect>
        <rect x="150" y="150" width="150" height="50" name="kjx">
            <rect x="0" y="0" width="50" height="50" name="k">
            </rect>
            <rect x="50" y="0" width="50" height="50" name="j">
            </rect>
            <rect x="100" y="0" width="50" height="50" name="x">
            </rect>
        </rect>
        <rect x="300" y="150" width="150" height="50" name="qz">
            <rect x="0" y="0" width="50" height="50" name="q">
            </rect>
            <rect x="50" y="0" width="50" height="50" name="z">
            </rect>
            <rect x="100" y="0" width="50" height="50" name=".">
            </rect>
        </rect>
    </rect>
</rect>
以下是QML文件的详细信息:

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQml.Models 2.2
import QtQuick.Window 2.2

ApplicationWindow {
    width: 480
    height: 640
    TreeView {
        id: treeView
        anchors.fill: parent
        anchors.margins: 6
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter

        model: tmodel

        TableViewColumn {
            title: "Name"
            role: "name"
            resizable: true
        }

        TableViewColumn {
            title: "Description"
            role: "description"
            resizable: true
        }
        TableViewColumn {
            title: "Top"
            role: "top"
            resizable: true
        }

        TableViewColumn {
            title: "Left"
            role: "left"
            resizable: true
        }
        TableViewColumn {
            title: "Width"
            role: "width"
            resizable: true
        }

        TableViewColumn {
            title: "height"
            role: "height"
            resizable: true
        }

        TableViewColumn {
            title: "Count"
            role: "count"
            resizable: true
        }
    }
}
问题:


是否可以重新使用QTreeView来显示这些矩形?如果没有,我可以使用转发器显示当前模型实现的矩形吗?我尝试使用Repeater,但子角色返回为QVariant列表,我不知道如何处理它。

我使用QStandarItemModel,而不是使用您的模型,因为它更易于使用(我避免测试您的代码)。如前所述,在QML的情况下,我使用了中继器、装入器和委托模型的组合

main.py

import os
import sys
from PyQt5 import QtCore, QtGui, QtQml
import xml.etree.ElementTree as ET


class XMLModel(QtGui.QStandardItemModel):
    def loadFromPath(self, filename, attributes):
        roles = {}
        for i, attr in enumerate(attributes):
            roles[QtCore.Qt.UserRole + i] = attr.encode()
        self.setItemRoleNames(roles)

        tree = ET.parse(filename)
        root = tree.getroot()
        self.parseXML(root)

    def parseXML(self, element, parent=None):
        if parent is None:
            parent = self.invisibleRootItem()
        it = QtGui.QStandardItem()
        parent.appendRow(it)
        for role, tag in self.roleNames().items():
            value = element.attrib[tag.data().decode()]
            it.setData(value, role)
        for child in element:
            self.parseXML(child, it)

if __name__ == '__main__':
    dir_path = os.path.dirname(os.path.realpath(__file__))
    app = QtGui.QGuiApplication(sys.argv)
    model = XMLModel()
    model.loadFromPath(os.path.join(dir_path, 'rect.xml'), ["name", "x", "y", "width", "height"])
    engine = QtQml.QQmlApplicationEngine()
    ctx = engine.rootContext()
    ctx.setContextProperty("tmodel", model)
    file_path = os.path.join(dir_path, 'simpletreemodel.qml')
    engine.load(QtCore.QUrl.fromLocalFile(file_path))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())
simpletreemodel.qml

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Window 2.2

ApplicationWindow {
    width: 480
    height: 640
    visible: true
    Repeater {
        model: RectDelegateModel{
            model: tmodel
        }
    }
}
import QtQml.Models 2.2
import QtQuick 2.5

DelegateModel {
    id: mainModel
    delegate: Rectangle{
        Repeater {
            id: repeater
            model: childrenLoader.item
        }
        x: model.x
        y: model.y
        width: model.width
        height: model.height
        color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
        Text {
            anchors.centerIn: parent
            text: model.name
        }
        Loader {
            id: childrenLoader
            asynchronous: true
        }
        Component.onCompleted: {
            if (model && model.hasModelChildren) {
                childrenLoader.setSource("RectDelegateModel.qml", {
                                             "model": mainModel.model,
                                             "rootIndex": mainModel.modelIndex(index)
                                         });
            }
        }
    }
}
RectDelegateModel.qml

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Window 2.2

ApplicationWindow {
    width: 480
    height: 640
    visible: true
    Repeater {
        model: RectDelegateModel{
            model: tmodel
        }
    }
}
import QtQml.Models 2.2
import QtQuick 2.5

DelegateModel {
    id: mainModel
    delegate: Rectangle{
        Repeater {
            id: repeater
            model: childrenLoader.item
        }
        x: model.x
        y: model.y
        width: model.width
        height: model.height
        color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
        Text {
            anchors.centerIn: parent
            text: model.name
        }
        Loader {
            id: childrenLoader
            asynchronous: true
        }
        Component.onCompleted: {
            if (model && model.hasModelChildren) {
                childrenLoader.setSource("RectDelegateModel.qml", {
                                             "model": mainModel.model,
                                             "rootIndex": mainModel.modelIndex(index)
                                         });
            }
        }
    }
}

一个问题:
子矩形相对于父矩形的位置是什么?例如,在xxx中,键盘位置是否相对于扫描位置?@eyllanesc是的,当显示矩形时,位置相对于父对象感谢您的帮助。正是我需要的