Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.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初始化和自定义QML组件_Python_Qml_Pyside6 - Fatal编程技术网

使用Python初始化和自定义QML组件

使用Python初始化和自定义QML组件,python,qml,pyside6,Python,Qml,Pyside6,是否有一种方法可以将QML组件视为对象,并在Python中初始化它们?例如,下面是一个用于长方体的简化QML: 我希望能够复制构造函数方法在Java中的功能。我希望能够通过Python脚本自定义每个框上的文本,同时创建彼此独立的多个框实例 import QtQuick 2.0 import QtQuick.Controls 2.0 Item { id: boxItem width: 800 height: 118 Rectangle { id

是否有一种方法可以将QML组件视为对象,并在Python中初始化它们?例如,下面是一个用于长方体的简化QML: 我希望能够复制构造函数方法在Java中的功能。我希望能够通过Python脚本自定义每个框上的文本,同时创建彼此独立的多个框实例

import QtQuick 2.0
import QtQuick.Controls 2.0

Item {
    id: boxItem
    width: 800
    height: 118

    Rectangle {

        id: boxRect
        height: 118
        color: "#55f555"
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: parent.top

        Text {
            id: textItem
            width: 463
            height: 43
            color: "#000000"
            text: qsTr("Header Text")
            anchors.left: parent.left
            anchors.top: parent.top
            font.pixelSize: 38
            verticalAlignment: Text.AlignVCenter
            font.family: "Roboto"
            textFormat: Text.AutoText
            anchors.leftMargin: 20
            anchors.topMargin: 20
        }
    }

}
这是我当前从Qt模板版本修改的Python脚本:

import os
import sys
from pathlib import Path

import PySide6.QtQml
from PySide6.QtQuick import QQuickView
from PySide6.QtCore import Qt, QUrl
from PySide6.QtGui import QGuiApplication


if __name__ == '__main__':

    #Set up the application window
    app = QGuiApplication(sys.argv)
    view = QQuickView()
    view.setResizeMode(QQuickView.SizeRootObjectToView)

    #Load the QML file
    qml_file = Path(__file__).parent / "Main.qml"
    view.setSource(QUrl.fromLocalFile(os.fspath(qml_file.resolve())))

    #Show the window
    if view.status() == QQuickView.Error:
        sys.exit(-1)
    view.show()

    #execute and cleanup
    app.exec()
    del view

快速澄清:我正在使用定制的QML组件,而不是试图编辑QT预先存在的组件。

应用另一种技术(编程语言、库、框架)的概念通常是使用其他技术的糟糕方法。每种技术都有自己的方法和良好实践来实现任何需求

在QML的情况下,通常认为C++、Python等其他语言实现业务逻辑(类似于后端),QML负责视图和操作。在这种情况下,建议创建一个QoObject,该QoObject提供创建、修改等数据的其他QoObject。这可以反映在视图中。在这种特殊情况下,对于需求,使用模型和自定义qml项就足够了

Box.qml

import QtQuick 2.0
import QtQuick.Controls 2.0

Item {
    id: boxItem
    width: 800
    height: 118

    property alias text: textItem.text

    Rectangle {

        id: boxRect
        height: 118
        color: "#55f555"
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: parent.top

        Text {
            id: textItem
            width: 463
            height: 43
            color: "#000000"
            anchors.left: parent.left
            anchors.top: parent.top
            font.pixelSize: 38
            verticalAlignment: Text.AlignVCenter
            font.family: "Roboto"
            textFormat: Text.AutoText
            anchors.leftMargin: 20
            anchors.topMargin: 20
        }
    }
}
import QtQuick 2.0
import QtQuick.Controls 2.0

Item {
    id: root
    ScrollView 
    {
        anchors.fill: parent
        Column{
            Repeater{
                model: manager.model
                Box{
                    text: model.display
                }
            }
        }
    }
}
main.qml

import QtQuick 2.0
import QtQuick.Controls 2.0

Item {
    id: boxItem
    width: 800
    height: 118

    property alias text: textItem.text

    Rectangle {

        id: boxRect
        height: 118
        color: "#55f555"
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: parent.top

        Text {
            id: textItem
            width: 463
            height: 43
            color: "#000000"
            anchors.left: parent.left
            anchors.top: parent.top
            font.pixelSize: 38
            verticalAlignment: Text.AlignVCenter
            font.family: "Roboto"
            textFormat: Text.AutoText
            anchors.leftMargin: 20
            anchors.topMargin: 20
        }
    }
}
import QtQuick 2.0
import QtQuick.Controls 2.0

Item {
    id: root
    ScrollView 
    {
        anchors.fill: parent
        Column{
            Repeater{
                model: manager.model
                Box{
                    text: model.display
                }
            }
        }
    }
}
main.py

导入操作系统
导入系统
从pathlib导入路径
来自PySide6.QtCore导入属性,QObject,Qt,QUrl
从PySide6.QtGui导入qgui应用程序、QStandardItemModel、QStandardItem
从PySide6.QtQuick导入QQuickView
班级经理(QObject):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.\u model=QStandardItemModel()
@属性(QObject,常数=True)
def型号(自):
返回自.\u模型
如果名称=“\uuuuu main\uuuuuuuu”:
#设置应用程序窗口
app=qgui应用程序(sys.argv)
视图=QQuickView()
view.setResizeMode(QQuickView.SizerootObject视图)
经理=经理()
view.rootContext().setContextProperty(“管理器”,管理器)
qml_file=Path(_file__).parent/“main.qml”
view.setSource(QUrl.fromLocalFile(os.fspath(qml_file.resolve()))
如果view.status()==QQuickView.Error:
系统出口(-1)
查看。调整大小(640480)
view.show()
对于范围(20)内的i:
item=QStandardItem(f“item-{i}”)
manager.model.appendRow(项)
app.exec()

应用另一种技术(编程语言、库、框架)的概念通常是使用其他技术的糟糕方法。每种技术都有自己的方法和良好实践来实现任何需求

在QML的情况下,通常认为C++、Python等其他语言实现业务逻辑(类似于后端),QML负责视图和操作。在这种情况下,建议创建一个QoObject,该QoObject提供创建、修改等数据的其他QoObject。这可以反映在视图中。在这种特殊情况下,对于需求,使用模型和自定义qml项就足够了

Box.qml

import QtQuick 2.0
import QtQuick.Controls 2.0

Item {
    id: boxItem
    width: 800
    height: 118

    property alias text: textItem.text

    Rectangle {

        id: boxRect
        height: 118
        color: "#55f555"
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: parent.top

        Text {
            id: textItem
            width: 463
            height: 43
            color: "#000000"
            anchors.left: parent.left
            anchors.top: parent.top
            font.pixelSize: 38
            verticalAlignment: Text.AlignVCenter
            font.family: "Roboto"
            textFormat: Text.AutoText
            anchors.leftMargin: 20
            anchors.topMargin: 20
        }
    }
}
import QtQuick 2.0
import QtQuick.Controls 2.0

Item {
    id: root
    ScrollView 
    {
        anchors.fill: parent
        Column{
            Repeater{
                model: manager.model
                Box{
                    text: model.display
                }
            }
        }
    }
}
main.qml

import QtQuick 2.0
import QtQuick.Controls 2.0

Item {
    id: boxItem
    width: 800
    height: 118

    property alias text: textItem.text

    Rectangle {

        id: boxRect
        height: 118
        color: "#55f555"
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: parent.top

        Text {
            id: textItem
            width: 463
            height: 43
            color: "#000000"
            anchors.left: parent.left
            anchors.top: parent.top
            font.pixelSize: 38
            verticalAlignment: Text.AlignVCenter
            font.family: "Roboto"
            textFormat: Text.AutoText
            anchors.leftMargin: 20
            anchors.topMargin: 20
        }
    }
}
import QtQuick 2.0
import QtQuick.Controls 2.0

Item {
    id: root
    ScrollView 
    {
        anchors.fill: parent
        Column{
            Repeater{
                model: manager.model
                Box{
                    text: model.display
                }
            }
        }
    }
}
main.py

导入操作系统
导入系统
从pathlib导入路径
来自PySide6.QtCore导入属性,QObject,Qt,QUrl
从PySide6.QtGui导入qgui应用程序、QStandardItemModel、QStandardItem
从PySide6.QtQuick导入QQuickView
班级经理(QObject):
def uuu init uuu(self,parent=None):
super()。\uuuu init\uuuu(父级)
self.\u model=QStandardItemModel()
@属性(QObject,常数=True)
def型号(自):
返回自.\u模型
如果名称=“\uuuuu main\uuuuuuuu”:
#设置应用程序窗口
app=qgui应用程序(sys.argv)
视图=QQuickView()
view.setResizeMode(QQuickView.SizerootObject视图)
经理=经理()
view.rootContext().setContextProperty(“管理器”,管理器)
qml_file=Path(_file__).parent/“main.qml”
view.setSource(QUrl.fromLocalFile(os.fspath(qml_file.resolve()))
如果view.status()==QQuickView.Error:
系统出口(-1)
查看。调整大小(640480)
view.show()
对于范围(20)内的i:
item=QStandardItem(f“item-{i}”)
manager.model.appendRow(项)
app.exec()

感谢您提供的解决方案,它对我所做的工作非常有效。如果你不介意的话,可以问一个简单的问题——我发现当你需要重复的物品时,使用中继器是很好的,这就是我在本例中所需要的。但是,如果我遇到不需要中继器但仍然需要访问
model:manager.model
属性才能自定义值的组件,这可能吗?@SCP3008您的要求不清楚,您能否给出一个具体的示例?不过,我问错了。我已经对代码进行了更多的实验,我想知道是否有可能引入其他类似上面的小部件?在上一个示例中,我只需要一个可自定义的框,现在我想知道如何在当前框组件的正下方添加一个新的可自定义组件,其中包含多个可自定义字段。我是否应该在一篇有代码示例的新文章中问一下?@SCP3008很多次的逻辑是python通过QObject或qproperty提供数据,并在这些对象之间进行绑定。例如,假设您将成为一个登录名,然后创建一个处理用户名和密码的qproperty,然后必须将项目的文本与qproperty绑定。我建议你检查一下,谢谢你的解决方案,它对我所做的工作非常有效。如果你不介意的话,可以问一个简单的问题——我发现当你需要重复的物品时,使用中继器是很好的,这就是我在本例中所需要的。但是,如果我遇到不需要中继器但仍需要访问
model:manager.model
属性才能自定义