Results 1 to 8 of 8

Thread: Simple model with QListView and QTableView

  1. #1
    Join Date
    Sep 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Simple model with QListView and QTableView

    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.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Simple model with QListView and QTableView

    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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. The following user says thank you to jefftee for this useful post:

    knro (28th September 2015)

  4. #3
    Join Date
    Sep 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Simple model with QListView and QTableView

    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?

  5. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Simple model with QListView and QTableView

    I would just use a QTableView as the only widget then since you only have three pieces of data to display.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  6. #5
    Join Date
    Sep 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Simple model with QListView and QTableView

    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.

  7. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Simple model with QListView and QTableView

    OK, then use whatever visual representation of the data you deem best...
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Simple model with QListView and QTableView

    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,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    knro (29th September 2015)

  10. #8
    Join Date
    Sep 2015
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Simple model with QListView and QTableView

    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!! :-)

Similar Threads

  1. Event handlers for QTableView model / selection model.
    By hickscorp in forum Qt Programming
    Replies: 2
    Last Post: 8th July 2011, 17:57
  2. Replies: 4
    Last Post: 1st April 2011, 23:44
  3. Replies: 1
    Last Post: 24th February 2011, 05:54
  4. A simple example of a tree model
    By YaK in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2009, 17:15
  5. QListView does not display its model
    By mtrpoland in forum Qt Programming
    Replies: 1
    Last Post: 22nd February 2008, 19:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.