Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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 PyQt+;自定义QML对象:无法读取属性';x';空的_Python_Qt_Pyqt_Qml - Fatal编程技术网

Python PyQt+;自定义QML对象:无法读取属性';x';空的

Python PyQt+;自定义QML对象:无法读取属性';x';空的,python,qt,pyqt,qml,Python,Qt,Pyqt,Qml,使用PyQt+QML,它非常直观。但是我有点困惑。qml绑定“window.current.summary”抱怨标题中存在错误,无法读取null的属性“summary” Python: class DataPoint(QObject): .... @pyqtProperty('QString') def summary(self): print("Retrieving summary: ", self._datapoint.summary)

使用PyQt+QML,它非常直观。但是我有点困惑。qml绑定“window.current.summary”抱怨标题
中存在错误,无法读取null的属性“summary”

Python:

class DataPoint(QObject):
    ....
    @pyqtProperty('QString')
    def summary(self):
        print("Retrieving summary: ", self._datapoint.summary)
        return self._datapoint.summary


class Weather(QObject):
    ....
    @pyqtProperty(DataPoint, notify=forecastChanged)
    def current(self):
        return DataPoint(self._forecast.currently())

    @pyqtProperty('QString', notify=forecastChanged)
    def current_summary(self):
        return self._forecast.currently().summary
QML:


我想我错过了一些明显的东西。我已经确认“当前”属性确实被询问过<代码>检索摘要从未出现,这向我表明数据点本身从未被询问过。我们如何使QML
w1.current.summary
按预期绑定?

我发现了问题。台词:

@pyqtProperty(DataPoint, notify=forecastChanged)
    def current(self):
        return DataPoint(self._forecast.currently())
他们是罪魁祸首。改为

@pyqtProperty(DataPoint, notify=forecastChanged)
    def current(self):
        return self._datapoint
其中,
self.\u datapoint
先前被设置为datapoint,则解决该问题


我从Python中的内存管理中得出,临时对象确实是非常临时的,更像C++(比C语言),临时-<代码>数据存储> /代码>在“代码< Currnter”(/Calp>< /P>)返回时,立即从范围和内存中消失,可以发布启动QML的部分,其余代码为AN,可能有错误,收到。我想不出一种方法来保持它的简洁(最少),但我会投入一些额外的精力来查看,例如,发布类DataPoint和Weather,main.py和.qmlI已经注意到更复杂的场景,它们不需要“self.\u DataPoint”部分。所以我认为我的结论有些不完整或错误。不过,解决办法确实有效

@pyqtProperty(DataPoint, notify=forecastChanged)
    def current(self):
        return self._datapoint