Results 1 to 3 of 3

Thread: Adding a new row with a QSortFilterProxyModel

  1. #1
    Join Date
    Oct 2015
    Posts
    17
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Adding a new row with a QSortFilterProxyModel

    I have a QTableView whose data model is a class derived from QSortFilterProxyModel that I have created myself. In this class we reimplemented the method filterAcceptsRow to be able to filter the data of the table according to several criteria. I can also sort the table by any of the fields in it.

    The problem arises when the user clicks the "Create" button, which creates a new empty row in the table. If I have a filter applied, the empty row does not appear because it does not meet this filter, which I do not want to happen because the user has to start to edit their data obligatorily.

    Also, when I have the table ordered by a field, when I add the empty row, it automatically positions itself in the position that it plays according to the sorting criteria, which is not desirable because I want it always to be in the first position.

    Any idea how you can fix this problem?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Adding a new row with a QSortFilterProxyModel

    A simple way would be to put some data in Qt::UserRole, in the newly created row items (or row items subjected to editing), then avoid filtering them (in filterAcceptsRow implementation) and fix the sorting position (in lessThan implementation), either top/bottom (or as you like).
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    Default Re: Adding a new row with a QSortFilterProxyModel

    Any idea how you can fix this problem?
    An alternative is to reverse the process: let the user enter the information into a form (not the table), then create a new row with the information when the user clicks the Add button. This also has the advantages of letting the user easily cancel the operation and of letting you validate the information before it gets to the model. If you add the row first and then edit directly in the table, it leads to the problems you are having and also makes it awkward for the user to back out.
    <=== 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. Replies: 0
    Last Post: 26th June 2014, 15:03
  2. Using QSortFilterProxyModel
    By Jennie Bystrom in forum Qt Programming
    Replies: 3
    Last Post: 6th December 2007, 11:28
  3. QSortFilterProxyModel
    By evgenM in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2007, 12:53

Tags for this Thread

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.