Edit vector elements when QListWidget Item is edited
-
Hi,
I have a vector of QString and i added all elements of this vector to the QListWidget. I want to edit elements of vectors by editing elements of QListWidget. I tried to handle this by using pointers but it doesnt help. I can only edit QlistWidget items, changes does not effect vector elements. I can achieve this by writing code that finds the which row of QlistWidged correspond to the which element of vector but i want to find out that is it possible to do it with direct access.
-
You need to dive into the Model-View-Controller pattern for that and use the QListView and QAbstractListModel.
Be easy on yourself if you work with those classes for the first time, it usually takes a bit until it really sinks in.
The point of this approach is to not duplicate the data in the displaying component (the view), but keeping it all in the back (the model – e.g. owning your vector) in one spot. So by editing something in the view, this editing transaction is directly passed to the model.Unfortunately Qt doesn't provide a compromise between full-fledged MVC, which creates quite an amount of overhead, and the ease-of-use of the QList/Table/TreeWidget classes.
-
I am not familiar with MVC, it seems comprehensive study necessary for learn it. So i found temporary solution for this project. Thanks for your answer.