December 16, 2010

borut123 borut123
Lab Rat
73 posts

how to access QList<QObject*>  items in QML

 

I pass QList<QObject*> from Qt to QML:
context->setContextProperty(“listViewModel”, QVariant::fromValue(data));

And then use it as Listview’s model to fill the ListView.
All that works correctly.

I also want to access model’s items from other places, not just from ListView
It should be possible to do it like that (for first item): listViewModel.get(0);

But I get the error:
TypeError: Result of expression ‘listViewModel.get’ [undefined] is not a function.

8 replies

December 17, 2010

Alexander Kuchumov Alexander Kuchumov
Lab Rat
379 posts

as variant, you can write your own qml element in C++ which will be wrapper on the listViewModel

December 17, 2010

Alexander Kuchumov Alexander Kuchumov
Lab Rat
379 posts

  1. class CustomObject : public QObject
  2. {
  3. ...
  4.    Q_INVOKABLE void setModel( QAbstractItemModel* ) {
  5.      QAbstractItemModel* _model
  6.  
  7.    }
  8.    Q_INVOKABLE QVariant item(int index) {
  9.       return _model->data( _model->index(index) );
  10.    }
  11. ...
  12. }

December 21, 2010

mbrasser mbrasser
Lab Rat
452 posts

Hi,

The initial release of QML for the most part focused on list/model support as a source of data for views. You can track issue QTBUG-14986 [bugreports.qt.nokia.com] for full list manipulation support from within QML.

Regards,
Michael

December 21, 2010

qtrahul qtrahul
Lab Rat
188 posts

You can see the how to access the list model element in QML.

You should check the below link.

http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html

Best regards,

December 23, 2010

QtK QtK
Lab Rat
1140 posts

qtrahul wrote:
You can see the how to access the list model element in QML.

You should check the below link.

http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html

Best regards,

Rahul,

Please read the query and reply accordingly.

August 19, 2012

raja26 raja26
Lab Rat
77 posts

Have you found any way to do this? Without writing doing complex QAbstractListModel?

August 20, 2012

borut123 borut123
Lab Rat
73 posts

No other way. Try QAbstractListModel, it’s not so difficult as it sounds.

August 21, 2012

raja26 raja26
Lab Rat
77 posts
borut123 wrote:
No other way. Try QAbstractListModel, it’s not so difficult as it sounds.

Its not difficult. But for doing simple things like this why should we sub class one more class and deal with defining properties, functions etc, etc. This is how I feel. I’m alread having a tough time dealing with poniters (Esp. deleting QThread objects). So I don’t want to create one more class. :-(

 
  ‹‹ [solved] How can I make something like Qt.createComponent(Rectangle) ?      [Solved]How to use Declarative Plugins ››

You must log in to post a reply. Not a member yet? Register here!