Issue with my QList that I am trying to set as a model for a QListView.
-
I have created a custom QObject that looks like this
class Budget : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged) ...
I am using this object to save the results of a query in a list as a
QList<QObject *>
and I then return it to my qml object.while (query.next()) { Budget *budget = new Budget(); budget->setName(query.value("glb_name").toString()); budget->setDescription(query.value("glb_description").toString()); ...
I have created my list view like this:
ListView { id: budgetListView delegate: ListItem.Subtitled { text: name subText: description valueText: value maximumLineCount: 3 } }
I do not immediately define the
model
since I want it to be created when I enter some search results. For that I have a button that when pressed will return the list from my c++ function to theListView
's model:budgetListView.model = MyApp.ListBudget(nameTextField.text ...
The thing is though that after I press the button I get a message that the various variables like
name
anddescription
are not defined.Why is this and how can I fix it?
-
Hi,
Because
name
,description
andvalue
here are considered as roles of your models, not the properties of your objects. So you are not accessing your objects directly but the model. -
Hi SGaist,
I tried changing the
ListView
so it looks like this:ListView { id: budgetListView delegate: ListItem.Subtitled { text: modelData.name subText: modelData.description valueText: modelData.value maximumLineCount: 3 } }
Something that does indeed remove the error messages, but after pressing the search button nothing really happens. I know the values are passed correctly since I can print them with something like
console.log(budgetListView.model[0].name);
. So I am not sure if it is possible to assign the model to myListView
later on (in this case at the press of a button), and if that List will update itself. -
How are you changing the model ?
-
@SGaist I am not sure how should I update it. What I do until now is have a button that calls a method located in a javascript file that calls the final query on my c++ class that returns a list (Sounds weird when I write it down).
Button
Button { id: registerButton text: "Search" textColor: Theme.primaryColor enabled: true onClicked: FormChecks.getBudgetList() }
Javascript
function getBudgetList() { budgetList = MyEngine.ListBudget(nameTextField.text, nameModifier.selectedText, ...
C++
I connect qml and c++ by usingqmlRegisterSingletonType<MyEngine>("AppManager", 0, 1, "MyEngine", MyEngine::qmlSingleton)
budgetList
is what I use as the model for myListView
.Im not sure that the
ListView
knows that the model is updated though. -
Indeed, it's not really clear what you are doing with
budgetList
.Out of curiosity, why do you create a new model ?
-
-
ListView is a QML component it's not a QListView.
In any case, I'd recommend implementing a model where you'll be able to execute these queries and modify the model itself like described here. You can even use a QSortFilterProxyModel if you need filtering.
-
I had a look at the
abstractitemmodel
example when I was starting to do the first tests but I dont think that it can work for me due to the way I connect qml with c++. If it would make it clearer then please have a look at the actual code here. The files in question aredatabasehandler.cpp
in Engine andListBudgetDemo.qml
in App. -
Since you are interfacing a SQL database, why not make use of the dedicate models ? That would simplify things.