Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PySide6 ListProperty causes segfault?

PySide6 ListProperty causes segfault?

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 1 Posters 290 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Keith Astoria
    wrote on last edited by
    #1

    I cannot get the PySide6 'ListProperty' to work correctly. I get segfaults on Ubuntu and on MacOS I get a hung Python process with no QT window displayed. I cannot test on Win10 because I cannot get PySide6 to install correctly.

    This is a stripped-down example that

    • Has a 'ListPropertyObj' that holds the ListProperty.
      • This is just supposed to be a simple 'list of wrapped strings'.
    • Has a simple string wrapper called 'ListPropertyElement' so I have a QObject to give to the ListProperty type
    • Simple ListView trying to use this ListProperty as a model.

    main.py

    import sys
    from pathlib import Path
    
    from typing import List
    
    from PySide6.QtCore import QObject, Slot, Property, Signal
    from PySide6.QtGui import QGuiApplication
    from PySide6.QtQml import QQmlApplicationEngine, QmlElement, ListProperty
    from PySide6.QtQuickControls2 import QQuickStyle
    
    QML_IMPORT_NAME = "io.qt.textproperties"
    QML_IMPORT_MAJOR_VERSION = 1
    
    @QmlElement
    class ListPropertyElement(QObject):
        _value: str
    
        def __init__(self, parent=None):
            super().__init__(parent)
            self._value = "N/A"
    
        def getValue(self):
            return self._value
    
        def setValue(self, value:str):
            self._value = value
    
        value = Property(str, fget=getValue, fset=setValue)
    
    @QmlElement
    class ListPropertyObj(QObject):
        
        _items: List[ListPropertyElement]
    
        def __init__(self, parent=None):
            super().__init__(parent)
            self._items = []
    
        def appendItem(self, item:ListPropertyElement):
            self._items.append(item)
    
        def countItems(self):
            return len(self._items)
        
        def itemAt(self, index:int):
            return self._items[index]
    
        items = ListProperty(ListPropertyElement,
                                count=countItems,
                                at=itemAt,
                                append=appendItem)
    
    
    if __name__ == '__main__':
        app = QGuiApplication(sys.argv)
        QQuickStyle.setStyle("Material")
        engine = QQmlApplicationEngine()
    
        qml_file = Path(__file__).parent / 'main.qml'
        engine.load(qml_file)
    
        if not engine.rootObjects():
            sys.exit(-1)
    
        sys.exit(app.exec())
    

    main.qml

    import QtQuick 2.0
    import QtQuick.Controls 2.1
    import QtQuick.Window 2.1
    import QtQuick.Controls.Material 2.1
    
    import io.qt.textproperties 1.0
    
    ApplicationWindow {
        id: page
        width: 800
        height: 400
        visible: true
        Material.theme: Material.Dark
        Material.accent: Material.Red
    
        ListPropertyObj {
            id: listPropertyObj
    
            items: [
                ListPropertyElement {
                    value: "one"
                },
                ListPropertyElement {
                    value: "two"
                }
            ]
        }
    
        ListView {
            model: listPropertyObj.items
    
            anchors.fill: parent
    
            delegate: Text {
                text: index
                color: "white"
            }
        }
    }
    

    You will note that I'm not even really accessing the list in this example. The ListView delegate is only accessing the 'index'. Perhaps I'm missing something but Google doesn't turn up much about ListProperty. Am I supposed to be using something else? If I don't try to access the ListProperty then I don't get the segfault.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved