PDA

View Full Version : How to assign Qlist as a model to ListView?



Mathan
5th October 2016, 15:42
Hi,

I read the values from XML file and store it in the object, and the object stored in the Qlist. Now I want to now how to assign the Qlist to Listview in qml?



HomeController.h:
-----------------------


struct PortalMapItemInfo {
QString pstrTitle;
QString pstrItemId;
QString pstrDescription;
QString pstrCreated;
};
class HomeController : public QObject
{

Q_OBJECT

public:

Q_INVOKABLE bool eveReadXML();

}


HomeController.CPP:
-----------------------


void HomeController::eveReadXML()
{

//Read the xml Value and load it the object of the class
PortalMapItemInfo obj;

obj.pstrTitle = strTitle;
obj.pstrItemId = strItemId;
obj.pstrDescription = strDescription;
obj.pstrCreated = strCreated;

QList<PortalMapItemInfo> datalist;

datalist << obj; //Values are properly binded.

}

ListPortalItem.Qml
--------------------


ListView {
id: idListView
anchors {
left: parent.left
leftMargin: 10 * scaleFactor
right: parent.right
rightMargin: 10 * scaleFactor
top: rectangleToolBar.bottom
topMargin: 10 * scaleFactor
bottom: rectangleStatusBar.top
bottomMargin: 10 * scaleFactor
}

// model:???????????????????????????
delegate: comsearchDelegate
spacing: 10 * scaleFactor
clip: true





}

Component {
id: comsearchDelegate

Row {
spacing: 10 * scaleFactor

Image {
id: imageItem
width: 100 * scaleFactor
height: 70 * scaleFactor
source: thumbnailUrl
}

Column {
Layout.alignment: Qt.AlignTop
Text { text: title; font { pixelSize: 14 * scaleFactor; bold: true } }

}
}
}

anda_skoa
6th October 2016, 16:31
Since your list entry has multiple value fields, I would suggest a QAbstractListModel as the base class instead of a simple QObject.

You have to implement roleNames() to generate a mapping from QML side role names to C++ role integer values, data to return the respective value and rowCount() as the size of the list.

Cheers,
_