[PyQt] SIGABRT when setting QQmlListProperty

Cody Scott cody at perspexis.com
Fri Jan 13 18:06:36 GMT 2017


I'm using @pyqtProperty(QtCore.QVariant) for the list property and it works
as long as I do .toVariant() in the setter.

One thing is that it calls the setter twice for each instance in this
example. I believe it should only call each setter once.

https://gist.github.com/Siecje/d1f2b270305762b4f73540c72adee6b8



On Fri, Jan 13, 2017 at 9:04 AM, Cody Scott <cody at perspexis.com> wrote:

> So what type should the @pyqtProperty have for lists of other types? For
> example a list of strings?
>
> On Fri, Jan 13, 2017 at 6:53 AM, Phil Thompson <
> phil at riverbankcomputing.com> wrote:
>
>> On 12 Jan 2017, at 9:05 pm, Cody Scott <cody at perspexis.com> wrote:
>> >
>> > I'm using PyQt5==5.7.1.
>> >
>> > I'm trying to set a list property from QML. I'm using
>> @pyqtProperty(QQmlListProperty)
>> > I'm getting an error that TypeError: list element must be of type
>> 'str', not 'NoneType' but they are strings in QML.
>> >
>> > After that I get a SIGABRT.
>> >
>> > Code and output here.
>> > https://gist.github.com/Siecje/0a6cb1b49358f9373496bd8952bfd1fd
>> >
>> > The gist and the attached file are the same.
>>
>> QQmlListProperty can only handle lists with elements that are sub-types
>> of QObject.
>>
>> I've improved the error checking.
>>
>> Phil
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20170113/849159a0/attachment.html>
-------------- next part --------------
import sys

from PyQt5 import QtCore, QtQml
from PyQt5.QtWidgets import QApplication


class CustomType(QtCore.QObject):
    names_changed = QtCore.pyqtSignal()
    def __init__(self, parent = None):
        super().__init__(parent)

        self._names = []
        self._times_in_setter = 0

    #@QtCore.pyqtProperty(QtQml.QJSValue, notify = names_changed)
    #@QtCore.pyqtProperty(list, notify = names_changed)
    @QtCore.pyqtProperty(QtCore.QVariant, notify = names_changed)
    def names(self):
        return self._names
    @names.setter
    def names(self, names):
        print(names)
        self._times_in_setter += 1
        names_list = names.toVariant()
        # if names_list == self._names:
        #     return
        self._names = names_list
        print("objectName: ", self.objectName())
        print("names: ", self._names)
        print("Times in setter: ", self._times_in_setter)
        self.names_changed.emit()
        print()


QML = b"""
import QtQuick 2.7
import QtQuick.Controls 1.4

import CustomType 1.0

ApplicationWindow {
    visible: true
    width: 800
    height: 600

    Column {
        ComboBox {
            model: customType.names
        }
        ComboBox {
            model: customType2.names
        }
    }

    CustomType {
      id: customType
      objectName: "customType"
      names: ["one", "two"]
    }
    CustomType {
      id: customType2
      objectName: "customType2"
      names: customType.names
   }
}
"""

app = QApplication(sys.argv)
QtQml.qmlRegisterType(CustomType, 'CustomType', 1, 0, 'CustomType')
engine = QtQml.QQmlApplicationEngine()
engine.loadData(QML)
app.exec_()


More information about the PyQt mailing list