PDA

View Full Version : Simple model with QListView and QTableView



knro
27th September 2015, 22:30
My data is arranged like this:
City1
+ Long1, Lat1
+ Long2, Lat2
City2
+ Long1, Lat1
+ Long2, Lat2
+ Long3, Lat3

So basically each city has a list of longitudes/latitudes associated with it. I want to display the Cities in a QListView and only the coordinates in a QTableView and have the table view contents gets updated when the user selects a different city. I'm not sure how to exactly approach this. Do I create a model with 3 columns (city, long, lat)? Do I create two models? What I did is that I created one model CityModel based on QStandardItemModel and then for each city I created QStandardItem and then used CityModel.appendRow(cityItem). Then I created two QStandardItem for Longitude/Latitude and added them to cityItem.appendRow(..), and the two views use the same model, but only the QListView displays the data, while the QTableView is empty (probably because the coordinates items are children of the city item).

I'm not sure how to proceed with this as I'm new to the model/view architecture and it's a bit confusing.

jefftee
28th September 2015, 00:06
Do you have more data to display other than city, longitude, and latitude? If not, I'd just use a QTableView.

If you do, you could use two widgets, a QTreeView on the left with City as the parent and each longitude/latitude as a child of the city. When you click on a specific longitude/lattitude child, then you could display the details in the widget on the right. The widget you choose would depend on the type/amount of data you need to display for each longitude/latitude, etc.

Hope that makes sense.

knro
28th September 2015, 00:28
Thanks for the reply. No there is no more data. And it is a lot simpler than this. There is no details to be viewed. I just wanted a way for the user to add cities and the corresponding longitudes/latitudes. The user can click on a globe and the long/lat are inserted into the QTableView (or enter the long/lat manually as well) and when they are done they can save the data. So it's a basic add/edit/delete widget for the cities and the corresponding longitudes/latitudes.

I added a QTreeView and I saw the children displayed, but they were still under the parent column "City", and nothing was in Long/Lat. I'm not sure how to make the data correspond with the correct headers in the tree widget.

Also, Is there a way to make the QTableView display only the long/lat list for each city in the QListView? or that involves setting up a proxy model of some sort? How about filtering by city?

jefftee
28th September 2015, 00:34
I would just use a QTableView as the only widget then since you only have three pieces of data to display.

knro
28th September 2015, 01:05
It's not city/long/lat, it's city + LIST of long/lat, could be 50 or 100 per city. The QTableView will be filled with the city repeated throughout, and it would be difficult to find a particular city in a very long QTableView.

jefftee
28th September 2015, 02:33
OK, then use whatever visual representation of the data you deem best... :)

anda_skoa
28th September 2015, 07:57
What does your data structure look like?
Do you have a list of City objects and each of those has a list of coordinate pairs?

In cases such as this is might be easiest to use two models. One derived from QAbstractListModel presenting the cities, and one derived from QAbstractTableModel presenting the coordinates for a given city.

In your current approach, with a tree model based on QStandardItem, you could try setting the same model for the table view and when selecting a city, set that model index as the table view's root index.

Cheers,
_

knro
29th September 2015, 19:30
In your current approach, with a tree model based on QStandardItem, you could try setting the same model for the table view and when selecting a city, set that model index as the table view's root index.

Thanks!!! That did the trick!! :-)