Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Returning a custom class from a model using remote objects and qml
Qt 6.11 is out! See what's new in the release blog

Returning a custom class from a model using remote objects and qml

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 412 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.
  • S Offline
    S Offline
    Suli Sahne
    wrote on last edited by
    #1

    Hi,

    Instead of assigning a role for each individual value of a model item and querying the values individually, is it possible to return an custom class from a model like this?

    QVariant MyModel::data(const QModelIndex& index, int role) const
    { 
        switch (role) {
        case MyModel::ItemRole: {
            return QVariant::fromValue<MyItem*>(m_items[index.row()]);
        }
        }
    
        return {};
    }
    

    The MyItem class derives from QObject, does all the Q_DECLARE_METATYPE, qRegisterMetaType, qRegisterMetaTypeStreamOperators stuff and the stream operator overloads also exist:

    QDataStream& operator<<(QDataStream& out, const MyItem* item);
    QDataStream& operator>>(QDataStream& in, MyItem* item);
    

    I've been trying for a while but somehow it doesn't seem to work. Whenever i want to access the items from qml

    GridLayout {
        ...
        Repeater {
            // Pointer to the MyModel replica of type QAbstractItemModelReplica
            model: myModelReplica
            delegate: Item {
                // _item is the name of the MyModel::ItemRole
                property var myItem: _item
                ...
            }
        }
    }                
    

    the operator>>() is called with a null pointer and after that a Out of memory occures:

    Out of memory  in C:\Users\qt\work\install\include\QtCore/qvector.h, line 709
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      https://wiki.qt.io/How_to_Use_a_Custom_Class_in_C%2B%2B_Model_and_QML_View

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      S 1 Reply Last reply
      1
      • VRoninV VRonin

        https://wiki.qt.io/How_to_Use_a_Custom_Class_in_C%2B%2B_Model_and_QML_View

        S Offline
        S Offline
        Suli Sahne
        wrote on last edited by
        #3

        @VRonin thank a lot for your help.

        When using Q_GADGET instead of the Q_OBJECT macro I don't have to derive from QObject and can therefore use value semantics. So the stream operator overloads can use a reference parameter and everything works perfectly:

        QDataStream& operator<<(QDataStream& out, const MyItem& item);
        QDataStream& operator>>(QDataStream& in, MyItem& item);
        
        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