Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficient loading ListModel data from Python #34

Closed
nadrimajstor opened this issue Jan 17, 2015 · 5 comments
Closed

Efficient loading ListModel data from Python #34

nadrimajstor opened this issue Jan 17, 2015 · 5 comments

Comments

@nadrimajstor
Copy link

In this tutorial video, at 39:40, Gabriel de Dietrich emphasized on inefficiency of the following pattern:

for (var i=0; i<result.length; i++) {
    listModel.append(result[i]);
}

In his case, he solved that by sharing the same data from the C++ code.
But C++ is all Greek to me - hence my enthusiasm about pyotherside 😃

In my case, I have a 10k+ list of annotated coordinates (a PCB with a lot of holes). User is supposed to do a lot of hole rearranging and the GUI responsiveness is essential to the workflow.
Presumably without diving in to the C++ code and staying on the py side, how changes to the ListModel can be more efficient?

@thp
Copy link
Owner

thp commented Jan 18, 2015

Yes, we should probably have some kind of list model class that can either store PyObject references or just qvariants. Do you want to modify the list from QML only or from Python or from both?

@thp thp closed this as completed Jan 18, 2015
@thp thp reopened this Jan 18, 2015
@nadrimajstor
Copy link
Author

I would like to modify the list from Python only.
Please note that due to my inexperience I'm might asking for a wrong feature. The goal is to keep a clean separation of UI from the py sided app logic i.e. all of the tests stay on the py side; as I intend to opensource it - others might want to use different UI (if any).
So, I'm thinking: User interactions are translated to appropriate py function calls; py sided logic make the changes to the list; QML side figures out that the ListModel has changed and redraws visual elements.
💭 Does this make sense?

@thp thp added the Brainstorm label Feb 19, 2015
@nadrimajstor
Copy link
Author

import QtQuick 2.0
import io.thp.pyotherside 1.4

Rectangle {
    id: root
    width: 200
    height: 200

    Component {
        id: fruitDelegate
        Row {
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 10
            Text { text: name } Text { text: '$' + cost }
        }
    }

    ListView {
        anchors.fill: parent
        id: fruitView
        delegate: fruitDelegate
    }

    Python {
        id: py
        Component.onCompleted: {
            importModule('other_side', function () {
                call('other_side.build_model', [], function (result) {
                    fruitView.model = Qt.createQmlObject(result, root, "dynamicFruitModel")
                });
            });
        }
    }
}
import pyotherside

def build_model():
    return 'import QtQuick 2.0; ListModel { id: fruitModelDynamic; ListElement { name: "Apple";  cost: 2.45 } ListElement { name: "Orange"; cost: 3.25 } ListElement { name: "Banana"; cost: 1.95 }}'

Seemingly a working example without JS looping on .append method. However I don't particularity like this "solution".
Is it feasible from the py side to expose e.g. a list of dicts as some form of QAbstractItemModel Class?

@basak
Copy link

basak commented Dec 5, 2015

I've gone in a different direction and implemented a way to implement a QAbstractItemModel in pure Python. Then a backend can be fully in Python, store the data in Python and access it pythonically, and the QML end just sees a regular model. Details in issue #52. I don't know how this will affect performance though, as it requires every data access from QML to go through Python (though not through a signal). However it may be that with an efficient model implementation, Qt won't keep re-reading the data, or won't retrieve data not in the present views?

@thp
Copy link
Owner

thp commented Jan 8, 2016

Superseded by #52

@thp thp closed this as completed Jan 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants