Unsolved get single item and it's property of QAbstractListModel in QML
-
@dheerendra i tried to get it to work with Qobject but it didnt worked. Also the Example from Qt (called abstractidemmodel bundelt with qt creator) didnt inhert from qobject.
void datasourcemodel::addDataSourceObject(const DataSourceObject &DataSourceObject){ beginInsertRows(QModelIndex(), rowCount(), rowCount()); m_DataSourceObjects << DataSourceObject; endInsertRows(); }
-
You don't need special methods to get stuff, see https://wiki.qt.io/How_to_Use_a_Custom_Class_in_C%2B%2B_Model_and_QML_View
-
m_DataSourceObjects
What is the datatype of above variable ? Can you show me the declaration ?
-
shouldn't be single items accessible with the data() function.
using the model in a listview works perfectly. But wenn access the value from a item directly in QML with this:
Text { id: text2 x: 600 y: 137 width: 100 height: 31 text: dataSourceModel.data(88,259) font.pixelSize: 12 }
Where 88 is the desired index and 259 the valueRole
qml returns: Unable to assign [undefined] to QString
this is my data function in my derived qabstractlistmodel class:
QVariant datasourcemodel::data(const QModelIndex & index, int role) const{ if(index.row() < 0 || index.row() >= m_DataSourceObjects.count()) return QVariant(); const DataSourceObject &dataSourceObject = m_DataSourceObjects[index.row()]; if (role == idRole) return dataSourceObject.id(); else if (role == nameRole) return dataSourceObject.name(); else if (role == displaynameRole) return dataSourceObject.displayname(); else if (role == valueRole) return dataSourceObject.value(); //else if (role == allRole) // return get(index.row()); return QVariant(); }
-
@Slash200 said in get single item and it's property of QAbstractListModel in QML:
shouldn't be single items accessible with the data() function
Not in the delegate, please read the wiki I posted.
-
-
You have model
-
This model contains the list of DataSource Object.
-
You got the values using QVariantList
-
Now you asked the question saying that value does not change when value is changed in the backend.
-
If you are working with View & Model, any change in model will reflect in view provided your model has appropriate signal.
-
What you wanted is access single value in model data & display it. Display is not ListView etc.
-
Whenever you change the value in Backend, it should change in UI also.
e.g
Text { text : dataModel.dataSourceObj.value }
This is how you wanted access it. If any change in the value should also change. data() method will be called through view. What data() method returns is only a value. Whenever any value changes, how it will reflect in your text UI ? So data(..) method will not of use.
You are trying to use model like object which gives the value.
So I suggested you to have method like following.Assuming that you store the object in the QList
TestObject* MyModel::get(int index) { DataSourceObj *obj = list.at(index) return obj }
I kept on asking show me how you store the DataSourceObj in model. Apart from this answer I get something else. Hence I was not able to give you get function details. Now I assumed you are storing as QList<> I have shown you the method signature.
I have requested you to inherit from QObject also. The reason is that property binding has to work with signal/slots. There is a way to do without QObject and use Q_GADGET. However this does not work for signal/slots.Hope things are clarified. Again I'm asking show me how do you store DataSource objects in Model. Which datastructure are you using to store DataSource Objects ?. If your problem is not the way I explained, then you are looking for something else & I did not understand your problem.
-
-
Apologies for the communication problems.
To simplify the whole thing I uploaded the project to github.There you see also the main.qml where i try the get the value of a object depending on the selected item with the combobox
-
is your problem here?
-
no in the main.qml
Text { id: text2 x: 600 y: 137 width: 100 height: 31 //text: dataSourceModel.get(cbx_sources.currentIndex).value text: dataSourceModel.data(cbx_sources.currentIndex,259) font.pixelSize: 12 }
i want to give the text2 the value of the selected item from combobox cbx_sources.
-
-
@dheerendra Thank you so much.
I adapted it to your suggestion.
Now I can access single data in the GUI.
Its working, but after hundrets of updates of the value of a object, it's returning NULL for no reason and the programm crashes.
when qmlRegisterType<DataSourceObject>(); is commented out, it's no more crashing
qrc:/main.qml:71: TypeError: Cannot read property 'value' of nulli debugged the object when it changes it value in printed its type and adress to the console:
It changes it's type?
Debug output:My adress is: DataSourceObject(0x201635b0) My adress is: DataSourceObject(0x201635b0) My adress is: DataSourceObject(0x201635b0) My adress is: DataSourceObject(0x201635b0) My adress is: DataSourceObject(0x201635b0) My adress is: DataSourceObject(0x201635b0) My adress is: QObject(0x201635b0) My adress is: QObject(0x201635b0) My adress is: QObject(0x201635b0) My adress is: QObject(0x201635b0) My adress is: QObject(0x201635b0)
The version is in the repo https://github.com/BastianGschrey/topic-97612
-
This is possible only if the one of the object is coming null in your model. You need to check why it so. Following snippet will help you to identify the issue.
e.g
TestObject* MyModel::get(int index) { DataSourceObj *obj = list.at(index) if (obj==nullpotr) { qCritical() << "Serious issue " << endl; obj = new DataSourceObj; // Create dummy object // put some dummy data into obj. This will come in your UI. } return obj }