Results 1 to 19 of 19

Thread: QSortFilterProxyModel trouble when inserting row

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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 

  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

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

  4. #4
    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?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.

  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

    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

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.

  8. #8
    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

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.