[SOLVED]How to get the value of a row in TableView using QML?
-
I know the property currentRow, but I want to get the value instead of the index.
Is there any method?
@CoderJeff Since you already have the index or row number
get
the value frommodel
. Ofcourse the method depends on which model type you are using. -
@CoderJeff Since you already have the index or row number
get
the value frommodel
. Ofcourse the method depends on which model type you are using.Yes, I tried to do it like what you said. But the model is from C++:
TableView {
id: tableView
......
model: myModelCpp.model()
......
}The type of myModelCpp is MySqlModel, which inherits QSqlTableModel.
Do I need to add a get function in C++ code?
-
Yes, I tried to do it like what you said. But the model is from C++:
TableView {
id: tableView
......
model: myModelCpp.model()
......
}The type of myModelCpp is MySqlModel, which inherits QSqlTableModel.
Do I need to add a get function in C++ code?
@CoderJeff Yes add a
Q_INVOKABLE
get (or any other suitable name) function. -
@CoderJeff Yes add a
Q_INVOKABLE
get (or any other suitable name) function.@p3c0
I feel it is another unreasonable design about QML.I think whatever and wherever the model is, once it is in QML, it should be converted to a kind of unified type like List or something else, and QML provides an unified interface that is convenient to call.
In addition, I tried to add the same code in ComboBox:
ComboBox {
......
model: myModelCpp.model()
......
}But it did not work, showing that the model is undefined.
Actually, either TableView or ComboBox, for us, their model are both a kind of data set, which should be used everywhere. Otherwise, we need to think about too much.
-
@p3c0
I feel it is another unreasonable design about QML.I think whatever and wherever the model is, once it is in QML, it should be converted to a kind of unified type like List or something else, and QML provides an unified interface that is convenient to call.
In addition, I tried to add the same code in ComboBox:
ComboBox {
......
model: myModelCpp.model()
......
}But it did not work, showing that the model is undefined.
Actually, either TableView or ComboBox, for us, their model are both a kind of data set, which should be used everywhere. Otherwise, we need to think about too much.
@CoderJeff I suspect you are definitely doing something wrong. Personally I have been using C++ models and never faced any major problems. Same C++ model in my case works perfectly with
ListView
andComboBox
. In case ofComboBox
you have to usetextRole
to populate but it is not related to modelundefined
.
Eg:ComboBox { model: myModel textRole: 'title' }
-
@CoderJeff I suspect you are definitely doing something wrong. Personally I have been using C++ models and never faced any major problems. Same C++ model in my case works perfectly with
ListView
andComboBox
. In case ofComboBox
you have to usetextRole
to populate but it is not related to modelundefined
.
Eg:ComboBox { model: myModel textRole: 'title' }
-
@p3c0
You are right. My ComboBox code works according to your method.QML is like an unearth buried treasure. As a beginner, I have a very long way to explore it.
Thank you p3c0.
@CoderJeff You're Welcome :)
I would suggest you to start from QML basics to make the roots strong.