Results 1 to 2 of 2

Thread: Same model different views QTableView

  1. #1
    Join Date
    Aug 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Same model different views QTableView

    Hi All,

    How can I use the same model but only use some of the data/columns in it?
    For example, Lets say the first column is "Name" and the second and third columns are "Number 1" and "Number 2" respectively.

    How can I display a second view that keeps the Name but adds the 2 numbers together and displays it?
    Is there a simple way of doing this?

    Model 1:
    Name| No1 | No2
    Bob | 1 | 2

    Model 2:
    Name| Total
    Bob | 3

    Thanks.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Same model different views QTableView

    Use a QSortFilterProxyModel (or a custom proxy model derived from QAbstractProxyModel) on your second view, with your original model set as its source model.

    In the proxy, you will filter out the two columns whose values you want to add, and substitute a new column with the sum. This means you'll probably have to implement columnCount(), data, headerData(), and maybe filterAcceptsColumn() to exclude the two source columns you are going to add together.

    I don't have a good example to give you, but there are plenty of variations if you google for it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Model with two very different views
    By mholmes in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2010, 23:19
  2. One Model, Two Views (QTreeView and QTableView)
    By dgarrett97 in forum Newbie
    Replies: 2
    Last Post: 14th September 2009, 18:10
  3. how to disable views from a model
    By zeeeend in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2008, 21:14
  4. 2 Views 1 Model
    By PrimeCP in forum Qt Programming
    Replies: 3
    Last Post: 2nd October 2007, 01:40
  5. Model Views and QLabel
    By naresh in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2006, 15:14

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.