Malachi
Malachi

Reputation: 2453

PyQt + custom QML object: Cannot read property 'x' of null

Using PyQt + QML, it's pretty intuitive. However I'm a little stumped. The qml binding 'window.current.summary' complains with the error in the title Cannot read property 'summary' of null

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:

Weather {
    id: w1
}

Text {
    ...
    id: current_temp
    text: w1.current.summary
    // text: w1.current_summary // this works
}

Think I've missed something obvious here. I've verified that the 'current' property is indeed interrogated. Retrieving summary is never seen, indicating to me that DataPoint itself is never interrogated. How do we get that QML w1.current.summary to bind as expected?

Upvotes: 1

Views: 827

Answers (1)

Malachi
Malachi

Reputation: 2453

I've discovered the problem. The lines:

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

Are the culprit. Changing to

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

Where self._datapoint is previously set to DataPoint solves it.

I conclude from this that the memory management in Python is such that temporary objects are very temporary indeed, more like C++ than C# - and the temporary-DataPoint immediately vanishes from scope and memory on return of current()

Upvotes: 1

Related Questions