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. How to use QList<CustomClass> in python with Shiboken
Qt 6.11 is out! See what's new in the release blog

How to use QList<CustomClass> in python with Shiboken

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 709 Views 1 Watching
  • 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.
  • H Offline
    H Offline
    HugoVS
    wrote on last edited by
    #1

    Hello,
    I have a Qt/C++ project that exposes a QList of Requirement to python (with Shiboken6).

    This is the requirement class

    namespace requirement{
    class Requirement
    {
    
    public:
      ...
      bunch of attributes
      ...
    };
    }
    

    This is another class witha slot using QList<Requirement>

    namespace requirement{
    class RequirementsModelBase{
    public Q_SLOTS:
    ...
        virtual void addRequirements( QList<Requirement> requirements);
    ...
    };
    }
    

    And Another class with a signal that I want to connect in python to the addRequirements slot

    namespace requirement{
    class RequirementsManager
    {
        Q_OBJECT
    ...
    Q_SIGNALS:
        void listOfRequirements(QList<requirement::Requirement>);
    ...
    };
    }
    

    this is the python script

    import sys
    
    from PySide6.QtWidgets import QApplication
    from PySide6.QtCore import SIGNAL, SLOT, QObject
    from PyTasteQtWidgets import TasteQtWidgets as QtTaste  # after PySide!
    
    def random_slot(value):
        print(dir(value))
    if __name__ == "__main__":
    
        app = QApplication(sys.argv)
    
    
        manager = QtTaste.RequirementsManager()
        model = QtTaste.RequirementsModelBase()
        manager.listOfRequirements.connect(random_slot)
        widget = QtTaste.RequirementsWidget("", manager, model)
        widget.show()
    
        sys.exit(app.exec())
    

    When the signal is emitted I get this error:

    TypeError: Can't call meta function because I have no idea how to handle QList<requirement::Requirement>
    

    for the sake of completeness, this is the typesystem xml file used with shiboken:

    <?xml version="1.0"?>
    <typesystem package="TasteQtWidgets">
        <load-typesystem name="typesystem_widgets.xml" generate="no"/>
        <load-typesystem name="typesystem_core.xml" generate="no"/>
        <load-typesystem name="typesystem_gui.xml" generate="no"/>
        <namespace-type name="requirement" visible="no">
            <object-type name="RequirementsWidget">
            </object-type>
            <object-type name="RequirementsManager">
            </object-type>
    	<value-type name="Requirement">
            </value-type>
            <object-type name="RequirementsModelBase">
                <enum-type name="RoleNames"/>
                <enum-type name="HEADER_SECTIONS"/>
            </object-type>
        </namespace-type>
    </typesystem>
    
    

    I didn't find any documentation to use QList with any custom types so maybe someone could give me some hints on how to proceed with this.

    Thank you

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      I haven't used shiboken yet but I am wondering whether the chapter on containers might be what you are looking for.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • F Offline
        F Offline
        friedemannkleint
        wrote on last edited by
        #3

        I think the problem is the invisible namespace. You then get a mismatch between the type names in the Qt meta type system and the ones in Python and so, the QVariant conversion fails.

        1 Reply Last reply
        1
        • H Offline
          H Offline
          HugoVS
          wrote on last edited by
          #4

          You were right or at least partially. We set the namespace visibility to yes and updated the Qt version.

          Now is working.

          Thank you everyone for your help.

          1 Reply Last reply
          0
          • H HugoVS has marked this topic as solved on

          • Login

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