Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QAbstractListModel return a QObject and use it in qml?
Forum Updated to NodeBB v4.3 + New Features

QAbstractListModel return a QObject and use it in qml?

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 6.9k 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.
  • B Offline
    B Offline
    bkamps
    wrote on last edited by
    #1

    Hi,

    I want to use the QAbstractListModel to fill a QML ListView and to set some properties of the ListView. But I also want to access the properties of the elements in my AbstractListModel.

    For Example:

    @class MyClass : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(int number READ Number WRITE SetNumber NOTIFY NumberChanged)
    }@

    @class MyClassListModel : public QAbstractListModel
    {
    Q_OBJECT

    public:
    MyClassListModel(QObject *parent = 0)
    : QAbstractListModel(parent){}

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;
    

    private:
    QList<MyClass*> mMyClasses
    }@

    @QVariant MyClassListModel::data(const QModelIndex &index, int role) const
    {
    if (!index.isValid())
    return QVariant();

    if (index.row() >= stringList.size())
        return QVariant();
    
    if (role == Qt::DisplayRole)
        return mMyClasses.at(index.row());  
    else
        return QVariant();
    

    }@

    return mMyClasses.at(index.row()); >>>>>>>>> Compiler tells me I cannot return this because it's not a QVariant. Not even when I register this class with Q_DECLARE_METATYPE(MyClass).

    How is this possible?

    This is the situation I want to archieve:

    I have read somewhere you can "return" a (pointer) QObject from data() which then is set in the QML Listview (as a delegate).

    If you then notify a property of that c++ QObject (MyClass number) the property will change in the QML ListView delegate.

    If the property of one of my c++ objects in the list changes I don't want to refresh the whole QML ListView (or add/remove rows) but only update the properties of that object.

    The QML object can then redraw itself instead of redrawing the whole list.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      What do you expect? You are returning a pointer to a MyClass object. You can wrap that in a QVariant if you want. You can not put an actual QObject in a QVariant, as that would require copying and QObjects can not be copied.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bkamps
        wrote on last edited by
        #3

        This is the situation I want to archieve:

        I have read somewhere you can “return” a (pointer) QObject from data() which then is set in the QML Listview (as a delegate).

        If you then notify a property of that c++ QObject (MyClass number) the property will change in the QML ListView delegate.

        If the property of one of my c++ objects in the list changes I don’t want to refresh the whole QML ListView (or add/remove rows) but only update the properties of that object.

        The QML object can then redraw itself instead of redrawing the whole list.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbrasser
          wrote on last edited by
          #4

          Hi,

          I'd suggest trying https://qt.gitorious.org/qt-labs/qml-object-model -- it has all of the underlying model stuff already implemented for you -- you just need to provide it with the list of objects.

          Regards,
          Michael

          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