[PyQt] Access Python QObject from QML fails to convert on second call

Fabian Sturm f at rtfs.org
Mon May 16 16:43:43 BST 2016


Hi!

I am hitting an obscure problem (bug?) with my Python3 QML program.
I implemented a QAbstractListModel with a custom get method to get to 
the underlying QObject items. The moment I try to get the same Python 
QObject at two different places in QML I get:

TypeError: unable to convert a Python 'QMyItem' object to a C++
'QObject*' instance

The get method looks like this:

@pyqtSlot(int, result=QMyItem)
def get(self, row):
    return self.find_q_my_item(row)

The model itself is attached to a ComboBox like this:

ComboBox {
    model: mymodel
    textRole: 'name'
    onModelChanged: currentIndex = Helper.getIndex(model, selectedItem)
    onActivated: selectedItem = model.get(index)
}

and the helper which iterates through the model until it finds the
selected item. This is necessary to set the combobox selected item in
the begining.

function getIndex(list, element) {
    if (list && element) {
        for (var i = 0; i < list.rowCount(); i++) {
            if (list.get(i).uuid === element.uuid) {
                return i
            }
        }
    }
    return -1
}

Any idea what the reason could be or how to debug it?

I also posted this on SO but I have no high hopes that someone knows
what is going on.

http://stackoverflow.com/questions/37257387/access-python-qobject-from-
qml-fails-to-convert-on-second-call


More information about the PyQt mailing list