How to assign Qlist as a model to ListView?
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:
-----------------------
Code:
struct PortalMapItemInfo {
};
class HomeController
: public QObject{
Q_OBJECT
public:
Q_INVOKABLE bool eveReadXML();
}
HomeController.CPP:
-----------------------
Code:
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
--------------------
Code:
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 } }
}
}
}
Re: How to assign Qlist as a model to ListView?
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,
_