Results 1 to 19 of 19

Thread: QSortFilterProxyModel trouble when inserting row

  1. #1
    Join Date
    Jan 2007
    Location
    The Netherlands
    Posts
    15
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default QSortFilterProxyModel trouble when inserting row

    Hi all,

    I'm having trouble using a proxy model in a tableview, everything was just fine when is used the qsqltablemodel directly. Though, using a proxy model, there are some issues.

    When I'm adding a row (code below) and edit the row which just has been added, the proxy model takes care of sorting the items again. This causes an empty row to appear, at the position at which the row was initially inserted.

    When I restart the application or manually set the model again in the tableview, it shows the items correctly again.

    Qt Code:
    1. int row = 0;
    2. model->insertRow(row);
    3. QModelIndex index = model->index(row, 1);
    4.  
    5. countriesView->setCurrentIndex(index);
    6. countriesView->edit(index);
    To copy to clipboard, switch view to plain text mode 

    model = QSortFilterProxyModel
    countriesView = QTableView

    Like I said, it used to work perfectly when model is an QSqlTableModel.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSortFilterProxyModel trouble when inserting row

    Sounds like the dynamic sorting causes that the edited model index is actually something different than the inserted one. I think you should add the row into the actual model and then use QAbstractProxyModel::mapFromSource() to get the correct model index.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2007
    Location
    The Netherlands
    Posts
    15
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel trouble when inserting row

    Thanks for your suggestion jpn, though it didn't work out as expected unfortunately.

    I currently use the following code instead of the code above to create a new row:

    Qt Code:
    1. int row = 0;
    2. model->sourceModel()->insertRow(row);
    3. QModelIndex index = model->sourceModel()->index(row, 1);
    To copy to clipboard, switch view to plain text mode 

    As you can see, I don't call edit() to enable the user to edit the field right away. So after adding a new row an empty row appears in the tableview, which can be edited when you double click on it. This itself works flawless, though when you're done editing and press enter to save the value it still adds an empty row at the top of the tableview. The table view is sorted correctly though.

    I hope you haven't run out of suggestions yet...
    Last edited by saknopper; 5th February 2007 at 10:16. Reason: spelling error

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSortFilterProxyModel trouble when inserting row

    Does this work as desired? Which exact version of Qt are you using?

    PS. Rows and columns can be added through the context menu..
    Attached Files Attached Files
    J-P Nurmi

  5. #5
    Join Date
    Jan 2007
    Location
    The Netherlands
    Posts
    15
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel trouble when inserting row

    Yes, it works perfectly.

    I'm using Qt 4.2.2

  6. #6
    Join Date
    Jan 2007
    Location
    The Netherlands
    Posts
    15
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel trouble when inserting row

    It's weird that when I calculate the position of the to be inserted row with model->rowCount() and then call insertRow() at that position. It still adds the row at the top of the table. While in your example in inserts the row at the bottom.

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSortFilterProxyModel trouble when inserting row

    Hmm, so maybe it happens only when using QSortFilterProxyModel + QSqlTableModel?
    J-P Nurmi

  8. #8
    Join Date
    Jan 2007
    Location
    The Netherlands
    Posts
    15
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel trouble when inserting row

    Perhaps... If that would be true, is there a way I can work around this problem that you know of?

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSortFilterProxyModel trouble when inserting row

    Could you, by any chance, prepare a minimal example reproducing the problem we could play with? You could for example modify the earlier example of mine to use an SQLite db or so..
    J-P Nurmi

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSortFilterProxyModel trouble when inserting row

    Can you provide a minimal compilable example reproducing the problem? I'd like to take a look...

  11. #11
    Join Date
    Jan 2007
    Location
    The Netherlands
    Posts
    15
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel trouble when inserting row

    Sure, I'll do that.

  12. #12
    Join Date
    Jan 2007
    Location
    The Netherlands
    Posts
    15
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel trouble when inserting row

    I made an example with an in memory sqlite database for you, it illustrates the problem very clearly (in my opinion).

    Some steps to reproduce the problem:

    1) Sort on country (instead of id)
    2) Add a new row
    3) Enter for example "Italy" in the textbox
    4) Press enter

    The view resorts itself, but there's an empty row at the top.

    Let me know if I can help you any further.

    Thanks for looking into it.
    Attached Files Attached Files

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSortFilterProxyModel trouble when inserting row

    Looks like the edit call causes the malfunction, probably because the "id" field is set to "0" instead of the proper index.

    I changed the addRow() method and it works fine now:
    Qt Code:
    1. void Table::addRow()
    2. {
    3. int row = proxyModel->sourceModel()->rowCount();
    4. proxyModel->sourceModel()->insertRow(row);
    5. QModelIndex idx = proxyModel->sourceModel()->index(row, 1);
    6. if (idx.isValid())
    7. {
    8. proxyModel->sourceModel()->setData(idx.sibling(idx.row(), 0), row); // added here
    9. idx = proxyModel->mapFromSource(idx);
    10. setCurrentIndex(idx);
    11. edit(idx);
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Jan 2007
    Location
    The Netherlands
    Posts
    15
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel trouble when inserting row

    I'll check it out when I get home, thanks for helping!

  15. #15
    Join Date
    Jan 2007
    Location
    The Netherlands
    Posts
    15
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel trouble when inserting row

    It's weird, but your solution which worked for you doesn't work for me.

    When I add your line of code to the example program and add a new country the id gets set immediately with a value that's already present in de database. After editing the table doesn't get sorted either.

    Are you also using Qt 4.2.2 with sqlite3?

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSortFilterProxyModel trouble when inserting row

    Quote Originally Posted by saknopper View Post
    When I add your line of code to the example program and add a new country the id gets set immediately with a value that's already present in de database. After editing the table doesn't get sorted either.
    Try replacing the whole slot as I also changed the model calls to reference the source model everywhere. Also try moving the setData call out of the if statement - that was where I placed it at first. There shouldn't be any difference but who knows...

    Are you also using Qt 4.2.2 with sqlite3?
    I'm using 4.2.0 here, but that shouldn't matter.

  17. #17
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSortFilterProxyModel trouble when inserting row

    I would have though that setting the column as "auto increment" could have solved the problem but seems not..
    J-P Nurmi

  18. #18
    Join Date
    Jan 2007
    Location
    The Netherlands
    Posts
    15
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QSortFilterProxyModel trouble when inserting row

    Quote Originally Posted by wysota View Post
    Try replacing the whole slot as I also changed the model calls to reference the source model everywhere. Also try moving the setData call out of the if statement - that was where I placed it at first. There shouldn't be any difference but who knows...
    Waarrghhh, I'm going crazy. Still the same. I copy -> pasted the whole function and also placed the setData call out of the if statement. No changes though.

    And indeed, adding AUTOINCREMENT doesn't solve it either.
    Last edited by saknopper; 6th February 2007 at 13:53. Reason: spelling error

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSortFilterProxyModel trouble when inserting row

    I tested against Qt4.2.2. Looks like it works if you set an id which is already there (row), but doesn't if you pass a correct one (row+1). Anyway this is just cased by the way the sqltablemodel works. The proxy is just confused because the index changes. Reseting the model should help, but you'll lose selection and position of the view as it will also be reset.

Similar Threads

  1. New to QT..need help guys..sorry for the trouble
    By neomax in forum General Discussion
    Replies: 2
    Last Post: 17th November 2006, 16:20
  2. QPixmap -> HICON trouble.
    By krivenok in forum Qt Programming
    Replies: 1
    Last Post: 11th August 2006, 15:51
  3. Problem inserting in QTableWidget
    By DPinLV in forum Qt Programming
    Replies: 2
    Last Post: 2nd August 2006, 00:10
  4. addDockWidget && removeDockWidget trouble
    By krivenok in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2006, 14:46
  5. Memory Trouble
    By nimrod in forum Newbie
    Replies: 4
    Last Post: 6th February 2006, 18:45

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.