November 5, 2011

RazrFalcon RazrFalcon
Lab Rat
114 posts

[SOLVED] How to add item to ListView?

Page  
1

I have ListView and I want to add new items by timer (for example).
I didn’t understand how…
I found only String ListModel Example [doc.qt.nokia.com] and AbstractItemModel Example [doc.qt.nokia.com]. But it’s static filling, not dynamic.

Example: every n second add new item to ListView with current time. How can I do this?
Thanks.

 Signature 

QT != Qt
Gentoo + KDE

17 replies

November 5, 2011

Volker Volker
Robot Herder
5428 posts

Use of QStandardItemModel comes into mind.

November 5, 2011

RazrFalcon RazrFalcon
Lab Rat
114 posts

And how to set QStandardItemModel to ListView?
Just like in “AbstractItemModel Example”?

I can’t understan how to update model: of ListView…

 Signature 

QT != Qt
Gentoo + KDE

November 5, 2011

Volker Volker
Robot Herder
5428 posts

just call

  1. QStandardItemModel *stdModel = new QStandardItemModel(this);
  2.  
  3. // populate the model here, if you want
  4.  
  5. listView->setModel(stdModel);

QStandardItemModel is a subclass of QAbstractItemModel, so you can use it for every item view (not for the item widgets, those do not need a model!). The QStandardItemModel has a basic usage example too.

If you want to add new entries to the model, just call the respective methods of QStandardItemModel, like appendRow, insertRow, etc.

November 5, 2011

RazrFalcon RazrFalcon
Lab Rat
114 posts

No, no, no!
I mean QML ListView! :)

 Signature 

QT != Qt
Gentoo + KDE

November 5, 2011

Volker Volker
Robot Herder
5428 posts

OMG – I completely missed that this is in the Qt Quick forums. I should have more coffee before posting weird stuff :-) – sorry for the confusion :)

November 5, 2011

RazrFalcon RazrFalcon
Lab Rat
114 posts

Never mind :)

 Signature 

QT != Qt
Gentoo + KDE

November 5, 2011

RazrFalcon RazrFalcon
Lab Rat
114 posts

Any ideas how to add items?

 Signature 

QT != Qt
Gentoo + KDE

November 5, 2011

Volker Volker
Robot Herder
5428 posts
RazrFalcon wrote:
Any ideas how to add items?

Sorry, I don’t have much experience with Qt Quick/QML so far – but I’m pretty sure someone else is able to help you.

November 6, 2011

michal.k michal.k
Lab Rat
70 posts

You can always use insert, append, clear methods of QML ListModel Element [doc.qt.nokia.com].

November 6, 2011

RazrFalcon RazrFalcon
Lab Rat
114 posts

I know, but I need to get items from QAbstractItemModel or other type of model.

 Signature 

QT != Qt
Gentoo + KDE

November 6, 2011

michal.k michal.k
Lab Rat
70 posts

Hmm, If you exposed your QAbstractItemModel derived model to QML like this

  1. QDeclarativeView* iView;
  2. //...
  3. //MyListModel derived from QAbstractItemModel
  4. MyListModel* iListModel;
  5.  
  6. //...
  7. iView->rootContext()->setContextProperty("listModel",iListModel);

and set it in qml list like this
  1. ListView {
  2. //...
  3.     model:listModel
  4. //...
  5. }

then every time you insert something to your model you have to call
  1. beginInsertRows(QModelIndex(),startPos,endPos);
  2. endInsertRows();

and your QML ListView will be updated

November 6, 2011

RazrFalcon RazrFalcon
Lab Rat
114 posts

Ok. I set my model to ListView. Thanks.
But how to add new item to this model, for example, after click on MouseArea.
Something like:

  1. MouseArea {
  2.     onClicked: myModel.append("text") // but here I get an error "TypeError: Result of expression 'myModel.append' [undefined] is not a function."
  3. }

 Signature 

QT != Qt
Gentoo + KDE

November 7, 2011

Andre Andre
Area 51 Engineer
6031 posts

You will have to supply slots on your model (or another class that you have also exposed to QML) that provide this functionality. However, because you are using a QAbstractItemModel, we have no idea what kind of model that is, so it is impossible to help you with the details. Are you sure you need to have your model in C++ though? I mean, if you just want to append simple texts, why not use one of the models QML supplies you?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

November 7, 2011

michal.k michal.k
Lab Rat
70 posts

Why not to use QML ListModel?

If you really want to do it using model exposed to QML from C++, then as Andre said you have to:
Make method which you call in onClicked a slot

  1. public slot:
  2.     void append(/*??*/);

or declare method as invokable
  1. Q_INVOKABLE void append(/*??*/);

November 7, 2011

RazrFalcon RazrFalcon
Lab Rat
114 posts

Thanks. It’s works.

Why not to use QML ListModel?

Becouse I need to use list items in my C++ code.
So QML – only GUI, all other works in C++ code. QML only for showing my items.
I think it’s correct way, is it?

 Signature 

QT != Qt
Gentoo + KDE

Page  
1

  ‹‹ JavaScript Date => QDateTime problems for dates prior to 1981      import com.nokia.symbian 1.0 -> Package not found ››

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