如何将对象从 python 返回到 QML?

发布于 2024-10-21 22:37:22 字数 571 浏览 3 评论 0原文

我正在尝试从插槽返回 QObject。我正在使用 PySide 和 QML。我的代码如下所示:

class myClass(QtCore.QObject):
    def __init__(self):
        self.object = QtCore.QObject()

    QtCore.Slot(result=object)
    def myFunc(self):
        return self.object

当我 console.log 时,我得到的返回值:

QVariant(PySide::PyObjectWrapper)

我似乎无法从这个东西中获取值。有人能指出我正确的方向吗?我可以返回原始类型(如 int、str 等),但对象、列表和字典超出了我的范围。任何帮助将不胜感激。

编辑

我正在尝试使用点表示法访问对象的属性。如果有人能给我举一个例子,那将会非常有帮助。不过,如果我能把东西拿回来,我想我可以自己走完这段距离。再次感谢!

谢谢杰克

I'm trying to return a QObject from a Slot. I'm using PySide and QML. My code looks like this:

class myClass(QtCore.QObject):
    def __init__(self):
        self.object = QtCore.QObject()

    QtCore.Slot(result=object)
    def myFunc(self):
        return self.object

When I console.log the return value I get:

QVariant(PySide::PyObjectWrapper)

I can't seem to get the value out of this thing. Can somebody point me in the right direction? I can return primitive types (like int, str, etc.), but objects, lists, and dicts are beyond me. Any help would be greatly appreciated.

EDIT

I am trying to access properties of an object using dot notation. If somebody could show me an example of this, it would be very helpful. However, if I could just get the object back I think I could go the distance by myself. Thanks again!

Thanks

Jack

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2024-10-28 22:37:22

所以,这就是我认为你想要做的事情。当您单击任意位置时,蓝色区域会变为绿色。这是通过更改信号传递的对象的颜色属性来完成的。

#!/usr/bin/env python

import sys
from PySide import QtCore
from PySide.QtGui import QApplication
from PySide.QtDeclarative import QDeclarativeView
from PySide.QtOpenGL import QGLWidget

def some_function(passed_object):
    passed_object.setProperty("color", "green")

def main(argv):
    app = QApplication(argv)

    display_widget = QDeclarativeView()
    display_widget.setViewport(QGLWidget())

    display_widget.setResizeMode(QDeclarativeView.SizeRootObjectToView)
    display_widget.setSource(QtCore.QUrl('pass_an_object.qml'))

    display_widget.rootObject().object_signal.connect(some_function)

    display_widget.show()
    display_widget.resize(640,480)

    sys.exit(app.exec_())

if __name__ == '__main__':
    main(sys.argv)

使用随附的 qml 文件(我将其称为 pass_an_object.qml):

import QtQuick 1.0

Rectangle {
    id: foo
    width: 640
    height: 640
    color: "red"

    signal object_signal(variant foo)

    Rectangle {
        id: an_object
        width: 100
        height: 100
        color: "blue"
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            object_signal(an_object)
        }

    }
}

So, here is something that does what I think you're trying to do. When you click anywhere, the blue area changes green. This is done by changing the color property on the object that is passed by a signal.

#!/usr/bin/env python

import sys
from PySide import QtCore
from PySide.QtGui import QApplication
from PySide.QtDeclarative import QDeclarativeView
from PySide.QtOpenGL import QGLWidget

def some_function(passed_object):
    passed_object.setProperty("color", "green")

def main(argv):
    app = QApplication(argv)

    display_widget = QDeclarativeView()
    display_widget.setViewport(QGLWidget())

    display_widget.setResizeMode(QDeclarativeView.SizeRootObjectToView)
    display_widget.setSource(QtCore.QUrl('pass_an_object.qml'))

    display_widget.rootObject().object_signal.connect(some_function)

    display_widget.show()
    display_widget.resize(640,480)

    sys.exit(app.exec_())

if __name__ == '__main__':
    main(sys.argv)

With the accompanying qml file (I've called it pass_an_object.qml):

import QtQuick 1.0

Rectangle {
    id: foo
    width: 640
    height: 640
    color: "red"

    signal object_signal(variant foo)

    Rectangle {
        id: an_object
        width: 100
        height: 100
        color: "blue"
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            object_signal(an_object)
        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文