August 17, 2011

spode spode
Ant Farmer
317 posts

[SOLVED] receiving list from c++ to qml listview

 

how to take a list from c++ to qml? in this case for example, i need to obtain the list from file.cpp:

  1. Component { id: delegate_grafica; Rectangle { width: 300; height: 30; color: "orange"; border.width: 1; border.color: "yellow"; Text { color: "black"; text: deutschesWort; font.pixelSize: 15; anchors.centerIn: parent } } }
  2.     ListView { id: list_parolaSalvate; x: 0; y: 0; width: 300; height: 300; delegate: delegate_grafica; }

e il “model”???

12 replies

August 17, 2011

kkrzewniak kkrzewniak
Lab Rat
218 posts

Please add some new lines to your code.

Expose a QAbstractListModel subclass as a context property or by using a Q_INVOAKABLE method.
Other options are using a QStringList or a QList<QObject> as the model.

Remember to register your QAbstractListModel subclass if you’ll not be exposing it via context property.
qmlRegisterUncreatableType/qmlRegisterType

 Signature 

Me, Grimlock, not “nice dino”. ME BASH BRAINS!

August 17, 2011

spode spode
Ant Farmer
317 posts

i will use “Other options are using a QStringList or a QList<QObject> as the model.”, but the problem is: how to pass this qlist to this QML file?
main.qml

  1. Rectangle  {
  2. width: 500; height: 500; signal emitsignal();
  3. Component { id: delegate_grafica; Rectangle { width: 300; height: 30; color: "orange"; border.width: 1; border.color: "yellow"; Text { color: "black"; text: deutschesWort; font.pixelSize: 15; anchors.centerIn: parent } } }
  4.     ListView { id: list_parolaSalvate; x: 0; y: 0; width: 300; height: 300; delegate: delegate_grafica; model: emitsignal() }
  5. }

here i need a model, that i want to obtain from emitsignal, by which i would to obtain a qlist that is then the container of the model of my listview {}. my qlist will contain qlist<biglietto>
where biglietto is:
  1. biglietto::biglietto(QString newcolor, int newnumberm, bool important){
  2. QString color = newcolor;
  3. int number = newnumber;
  4. bool isImportant = important;
  5. etc.
  6. }

however, thank yoU!

August 31, 2011

sriks sriks
Lab Rat
122 posts

  1.     QDeclarativeContext* context = declarativeView.rootContext();
  2.     context->setContextProperty("mymodel",(QObject*)&myModel); // here myModel is the QObject that exposes your model

  1. MyModel: public QObject {
  2. ...
  3. Q_INVOKABLE QStringList dataModel();
  4. }

In Qml

  1. ListView {
  2. ...
  3. model: myModel.dataModel();
  4. }

August 18, 2012

raja26 raja26
Lab Rat
72 posts

But still how can I access the model data in delegate?

August 18, 2012

spode spode
Ant Farmer
317 posts

You need to write in the delegate the name of a public variable contained in the model, which you associate with QDeclarativeView.contextProperty.setContextProperty(nameOfTheModel, listThatyouhavemade). Note the you have to use first that I have just written and then set the source of the qml file where the model is needed.

August 18, 2012

raja26 raja26
Lab Rat
72 posts

I found that a simple

  1. modelData
in the delegate does it for QStringList.
spode wrote:
You need to write in the delegate the name of a public variable contained in the model, which you associate with QDeclarativeView.contextProperty.setContextProperty(nameOfTheModel, listThatyouhavemade). Note the you have to use first that I have just written and then set the source of the qml file where the model is needed.

August 20, 2012

spode spode
Ant Farmer
317 posts
I have not understood. could you please explain youself better?

August 21, 2012

raja26 raja26
Lab Rat
72 posts

To access the list of

in that QStringList I have used
  1. modelData.
  2.  
  3. Like this:
  4.  
  5. ListView {
  6.   model: stringListPassedFromCpp
  7.   delegate: Text {
  8.     text: modelData
  9.     }
  10. }

August 21, 2012

spode spode
Ant Farmer
317 posts

I do not understand..

August 22, 2012

raja26 raja26
Lab Rat
72 posts

Look at my above code. The ‘modelData’ gives the QString stored in QStringList. So that I can use ‘modelData’ to access the QString stored inside the QStrlingList.

I have used QStringList as the model for QML ListView. Have you understood now?

August 22, 2012

spode spode
Ant Farmer
317 posts

Ok. Now i have understood! :)

August 22, 2012

raja26 raja26
Lab Rat
72 posts

@spode: My pleasure.. :-)

 
  ‹‹ connecting qml complex signal to qt slot      QML WebView Select Dialog Issue (Appearing off screen) ››

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