1

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?

3
  • you could publish the part that launches the .qml and the remaining code to be an minimal reproducible example, there could be the error. Commented May 24, 2018 at 20:59
  • roger that. I couldn't think of a way to keep it concise (minimal) but I'll put in some extra effort to see Commented May 24, 2018 at 23:59
  • 1
    For example, publish the class DataPoint and Weather, the main.py and the .qml Commented May 25, 2018 at 0:00

1 Answer 1

1

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()

Sign up to request clarification or add additional context in comments.

1 Comment

I have noticed more complex scenarios which don't demand the "self._datapoint" part. So I think there is something incomplete or erroneous about my conclusion. The fix does work though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.