28 December 2019
29 December 2019
30 December 2019
I have a QML + PySide2 sketch with ListView and SwipeView sharing the same model - subclass of QAbstractListModel. List represents, well, a list of projects and SwipeView represents the selected project in expanded view with control buttons, elements, etc. Each element of the model is QObject subclass and I have no problems accessing its Python properties from QML using @Property() decorator. But how can I call its methods? Yea, sure, I can define @Slot in QAbstractListModel and it will be called with the given index, and then redirect the action to my underlying QObject subclass. But it is definetely not very elegant solution. Then, I will need to return some data (logs) back to QML and what? Will be need to create some global handler that will dispatch the message to the widget by its index, access widgets hierarchy... Doesn't look like a legit architecture at all actually.
Ссылка
click to show
click to show
I was referring to this tutorial @ussserrr https://doc.qt.io/qtforpython/tutorials/qmlapp/qmlapplication.html
31 December 2019
So, I finally have a proof-of-concept now. The code is kind of messy currently cause I've tried so many things to get it work. In two words: to be able to directly invoke methods of the internal model (`QObject`-derived element inside a ListView`) I define property of my custom registered type and then simply reference it in QML handlers (such as `onClicked`). The tricky part was to add `result argument in Slot definition, otherwise the call returns nothing. And to emit a signal from the Python internal model and catch it in QML ListView element I've used a straightforward signal:
Connections {
target: listItem // this is a property representing a single list element
onLogAdded: {
log.append(message);
}
}