QAbstractListModel - dataChanged
-
Try replacing
if(role==Qt::EditRole)
byif(role==ValueRole)
in yoursetData
method.Are you sure your data is properly writed in
labels_p
in your code (checked with the debugger or a simple qDebug)?How do you write your
value
data in your qml? -
Try replacing
if(role==Qt::EditRole)
byif(role==ValueRole)
in yoursetData
method.Are you sure your data is properly writed in
labels_p
in your code (checked with the debugger or a simple qDebug)?How do you write your
value
data in your qml?@Julien-B said in QAbstractListModel - dataChanged:
Try replacing if(role==Qt::EditRole) by if(role==ValueRole) in your setData method.
Are you sure your data is properly writed in labels_p in your code (checked with the debugger or a simple qDebug)?Now the values get updated in the labels_p. You were right. Before I corrected the typing error it worked too :D
-
GridView { id:contentView anchors.fill: parent cellWidth: 200 cellHeight: 105 flow: GridView.FlowLeftToRight layoutDirection: Qt.LeftToRight verticalLayoutDirection: GridView.TopToBottom model: dataModel delegate: gridViewtemplate }
...
Component { id: gridViewtemplate Rectangle { width: 190 height: 85 color: "#4c4c4c" Label { id:lname anchors.horizontalCenter: parent.horizontalCenter color: "#cccccc" anchors.margins: 5 anchors.top: parent.top text: labelname font.pixelSize: 20 } Label { anchors.left: parent.left color: "#cccccc" anchors.bottom: parent.bottom anchors.margins: 5 text: value font.pixelSize: 25 Label { anchors.left: parent.right color: "#cccccc" anchors.verticalCenter: parent.verticalCenter anchors.margins: 5 text: unit font.pixelSize: 10 } } } }
In my main.cpp I set the context property datamodel to an object of my Page class.
engine.rootContext()->setContextProperty("dataModel",connector.data2);
connector is the class where i implemented my sockets and where I have the data2-object, which is a Page object. I am aiming for displaying the data2 object in the gridview.
The connector runs in a second thread. -
GridView { id:contentView anchors.fill: parent cellWidth: 200 cellHeight: 105 flow: GridView.FlowLeftToRight layoutDirection: Qt.LeftToRight verticalLayoutDirection: GridView.TopToBottom model: dataModel delegate: gridViewtemplate }
...
Component { id: gridViewtemplate Rectangle { width: 190 height: 85 color: "#4c4c4c" Label { id:lname anchors.horizontalCenter: parent.horizontalCenter color: "#cccccc" anchors.margins: 5 anchors.top: parent.top text: labelname font.pixelSize: 20 } Label { anchors.left: parent.left color: "#cccccc" anchors.bottom: parent.bottom anchors.margins: 5 text: value font.pixelSize: 25 Label { anchors.left: parent.right color: "#cccccc" anchors.verticalCenter: parent.verticalCenter anchors.margins: 5 text: unit font.pixelSize: 10 } } } }
In my main.cpp I set the context property datamodel to an object of my Page class.
engine.rootContext()->setContextProperty("dataModel",connector.data2);
connector is the class where i implemented my sockets and where I have the data2-object, which is a Page object. I am aiming for displaying the data2 object in the gridview.
The connector runs in a second thread. -
GridView { id:contentView anchors.fill: parent cellWidth: 200 cellHeight: 105 flow: GridView.FlowLeftToRight layoutDirection: Qt.LeftToRight verticalLayoutDirection: GridView.TopToBottom model: dataModel delegate: gridViewtemplate }
...
Component { id: gridViewtemplate Rectangle { width: 190 height: 85 color: "#4c4c4c" Label { id:lname anchors.horizontalCenter: parent.horizontalCenter color: "#cccccc" anchors.margins: 5 anchors.top: parent.top text: labelname font.pixelSize: 20 } Label { anchors.left: parent.left color: "#cccccc" anchors.bottom: parent.bottom anchors.margins: 5 text: value font.pixelSize: 25 Label { anchors.left: parent.right color: "#cccccc" anchors.verticalCenter: parent.verticalCenter anchors.margins: 5 text: unit font.pixelSize: 10 } } } }
In my main.cpp I set the context property datamodel to an object of my Page class.
engine.rootContext()->setContextProperty("dataModel",connector.data2);
connector is the class where i implemented my sockets and where I have the data2-object, which is a Page object. I am aiming for displaying the data2 object in the gridview.
The connector runs in a second thread. -
When I update the context property after adding the labels it works. But this shows me that my addLabel function has a mistake or do i have to emit a signal when i add an item to my QList ?
I thought the insertRows function do that.Thanks a lot for your help :)
-
When I update the context property after adding the labels it works. But this shows me that my addLabel function has a mistake or do i have to emit a signal when i add an item to my QList ?
I thought the insertRows function do that.Thanks a lot for your help :)
@goxx00 Basically the data() method should work because data is shown once. Unless you want to edit the values through the GUI (make them user editable) you don't need the setData function. You can just read or receive the values from the socket, store them in some form and then decide in your reading/receiving code what kind of signal should be emitted to notify the view about the changes. If the amount of rows stays the same, emit dataChanged with the indexes of the first and the last changed row. If rows are added, call begin/endInsertRows(). If much of the data is changed and the amount of rows changes, it's easiest to call begin/endResetModel().
When the view receives a signal it automatically decides how to fetch and update the visible data. In any case it calls your model's data() function.
I assume you have read the Subclassing section of the documentation and implemented rowCount and roleNames functions (probably it wouldn't work the first time unless you haven't).
-
Ok I am not ready yet :D I have the next problem maybe you can help me with it.
When I add an Item to the QList, with the addLabel function I get an error:QObject::connect:: Cannot queue arguments of type 'QQmlChangeSet' (Make sure 'QQmlChangeSet' is registered using qRegisterMetaType().)
I searched for a solution but I didnt found sth. that worked.
I thin k it has to do with the fact that the object is in another Thread than the GUI.