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

  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

    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

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

  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

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

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.