Unsolved Using 2 different ListModels with 2 different ComboBoxes
-
@sierdzio
I need to access 2 different tables in the same db. Should I use like patientModel instead of listModel in the second ComboBox likemodel: patientModel { id: listPatient }
?
Thank you for your help. -
I have no idea what your
ListModel
orPatientModel
classes do, so I can't answer that.But if these 2 combo boxes need to display different data, then you definitely need 2 different models. Or at least 2 different instances of the same model with some different flags set so that they will know to serve different data.
-
@sierdzio
Would you please show me an example how to create 2 different instances of the same ListModel to serve data from 2 different tables from the same database. I tried to give them different ids but, that clearly doesn't work.
Thank you. -
@gabor53 said in Using 2 different ListModels with 2 different ComboBoxes:
ListModel
Wait wait wait, you are using stock ListModel? That is a static model, it does not connect to any database whatsoever.
What you need is some subclass of QSqlQueryModel or QSqlTableModel (or custom QAbstractItemModel if you dare), set it up so that it gets correct data from DB, then expose it to QML and then it will work.
Something like:
class QueryModel: public QSqlQueryModel { Q_OBJECT public: explicit QueryModel(QObject *parent = nullptr) : QSqlQueryModel(parent) { setQuery("SELECT someColumn FROM SomeTable", QSqlDatabase::database("my_db")); } }; //... qmlEngine->rootContext()->setContextProperty("myQueryModel", queryModel); // QML model: myQueryModel
-
@sierdzio
I used this to connect the db to the model://Read data from MedNamesTable function dbGetMeds() { console.log("Entered dbGetMeds.") var db = dbGetHandle() db.transaction(function (tx) { var results = tx.executeSql( "SELECT MednameField FROM MedNamesTable order by MednameField asc") for (var i = 0; i < results.rows.length; i++) { listModel2.append({ "MednameField": results.rows.item( i).MednameField, "checked": "" }) } }) }
It works in the first ComboBox but not in the second.
-
Ah ok that makes it clear.
The reason why it does not work is that you only modify
listModel
orlistModel2
in your DB functions. If you want the data to be present in both objects, you need to modify both of them, so something like:let obj = { "MednameField": results.rows.item(i).MednameField, "checked": "" } listModel.append(obj) listModel2.append(obj)
I still would say it's better to use the c++ classes for this purpose, though. Hacking ListModel seems like a temporary solution at best.
-
@sierdzio
No I understand. Thank you for your help. -
@sierdzio
I made the following changes:function dbGetMeds() { console.log("Entered dbGetMeds.") var db = dbGetHandle() db.transaction(function (tx) { var results = tx.executeSql( "SELECT MednameField FROM MedNamesTable order by MednameField asc") for (var i = 0; i < results.rows.length; i++) { let obj = { "MednameField": results.rows.item(i).MednameField, "checked": "" } } }) }
model: ListModel{ id: medModel } Text { id: meds text: medModel.append(obj) font.pixelSize: 18 color: Setup.delegateText() anchors.verticalCenter: parent.verticalCenter }
I get no error message at all but there is nothing in the medCombo dropdown either. Am I still missing something?
Thank you. -
@gabor53 said in Using 2 different ListModels with 2 different ComboBoxes:
Am I still missing something?
Yes, you are not modifying the model at all now (it is empty). You need to keep those
append()
calls in your loop!for (var i = 0; i < results.rows.length; i++) { let obj = { "MednameField": results.rows.item(i).MednameField, "checked": "" } medModel.append(obj) // HERE! }
-
@sierdzio
I made the new modifications like this://Read data from MedNamesTable function dbGetMeds() { console.log("Entered dbGetMeds.") var db = dbGetHandle() db.transaction(function (tx) { var results = tx.executeSql( "SELECT MednameField FROM MedNamesTable order by MednameField asc") for (var i = 0; i < results.rows.length; i++) { let obj = { "MednameField": results.rows.item(i).MednameField, "checked": "" } medModel.append(obj) } }) }
delegate: QQuick2.ItemDelegate { width: medCombo.width height: medCombo.height //Defining the items in the dropdown list Text { id: meds text: medModel.MednameField font.pixelSize: 18 color: Setup.delegateText() anchors.verticalCenter: parent.verticalCenter } onClicked: medCombo.currentIndex = index highlighted: medCombo.highlightedIndex === index }
Now it pulls the first item from the MedNamesTable and nothing else. Is
text: medModel.MednameField
incorrect or something else? Thank you for your help.
-
Try calling it with lowercase first letter
mednameField
. QML engine might scoff at a property beginning with capital letter.Code looks OK so if it displays just one entry - maybe there is only one row in your SQL table?
-
@sierdzio
Same problem. Regardless whether it is MednameField or mednameField it only pulls the last item from the db. I applied it to the patient combobox too with the same results. Even if I delete thetext: medModel.MednameField
I get the same results. I am really puzzled now...