Results 1 to 11 of 11

Thread: Problems with QSortFilterProxyModel

  1. #1
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Question Problems with QSortFilterProxyModel

    Hi there guys,

    I have a bit of a problem here I have a tiny problem here. I have a database connected to a QSqlTableModel, which in turn is connected to a QSortFilterProxyModel which in turn is connected to a QTableView. I also have the following code for deleting records:

    Qt Code:
    1. void mainwindow::deletedialog() {
    2. QMessageBox::StandardButton button;
    3. button = QMessageBox::question(this, tr("Delete Record"),
    4. QString (tr("Are you sure you wish to delete this record?")),
    5. QMessageBox::Yes | QMessageBox::No);
    6.  
    7. if (button == QMessageBox::Yes) {
    8. //Declaring an item selection which we will use to see which row the user has selected
    9. QItemSelection selection = ui->View->selectionModel()->selection();
    10. QList<int> rows;
    11. foreach ( const QModelIndex & index, selection.indexes() ) {
    12. rows.append( index.row() );
    13. }
    14.  
    15. qSort( rows );
    16.  
    17. int prev = -1;
    18. for( int i = rows.count() - 1; i >= 0; i -= 1 ) {
    19. int current = rows[i];
    20. if( current != prev ) {
    21. model->removeRows( current, 1 );
    22. prev = current;
    23. }
    24. }
    25. }
    26.  
    27.  
    28. else {
    29. QMessageBox::information(this, tr("Delete Record"), tr("Please select the record you wish to delete"));
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    This code works fine unless we filter the contents of the table. If we have say 600 rows i the table, and we filter everything until we obtain 1 single record, then the code above may not delete the correct record. How can I make the above code work with QSortFilterProxyModel?

    Any sample code would be greatly appreciated.

    Thanks in advance,

    Nefastious

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QSortFilterProxyModel

    Use QSortFilterProxyModel::mapToSource() to get the right index of you original model.

  3. The following user says thank you to Lykurg for this useful post:

    Nefastious (31st October 2009)

  4. #3
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problems with QSortFilterProxyModel

    How exactly should I do that?

    like this?

    Qt Code:
    1. ...
    2. proxyModel->mapToSource();
    3. model->removeRows( current, 1 );
    4. ...
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with QSortFilterProxyModel

    originalindex = proxyModel->mapToSource(proxyindex);

    where both originalindex and proxyindex are of type QModelIndex

  6. The following user says thank you to squidge for this useful post:

    Nefastious (31st October 2009)

  7. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QSortFilterProxyModel

    Quote Originally Posted by Nefastious View Post
    How exactly should I do that?

    like this?

    Qt Code:
    1. ...
    2. proxyModel->mapToSource();
    3. model->removeRows( current, 1 );
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Before you just guess how to do, just follow the link and read:
    QModelIndex QSortFilterProxyModel::mapToSource ( const QModelIndex & proxyIndex ) const
    so in your case:
    Qt Code:
    1. foreach ( const QModelIndex & index, selection.indexes() ) {
    2. rows.append( ui->View->model()->mapToSource(index).row() );
    3. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to Lykurg for this useful post:

    Nefastious (31st October 2009)

  9. #6
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Question Re: Problems with QSortFilterProxyModel

    I'm sorry I'm going to ask this I'll probably make you all very angry at me for being such a rookie... but the following code:

    Qt Code:
    1. index = proxyModel->mapToSource(proxyModel->index());
    To copy to clipboard, switch view to plain text mode 

    just doesn't work. I get an error(/Users/k12yp70n/MedCare/MedAdmin/mainw.cpp:229: error: no matching function for call to 'QSortFilterProxyModel::index()'). I don't know what I am doing wrong. Please correct me but, find a solution having in mind the code I initially posted.

    Thank you... and sorry for being such a newbie...

  10. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QSortFilterProxyModel

    What do you expect to get by "proxyModel->index()"? the selected indices you get by the selection model how you have written in your example code. And by the way: Have you written it by yourself or just copy and pasted id? Have you tried my suggestion with your code? (supposing you have set the proxy model...)

  11. The following user says thank you to Lykurg for this useful post:

    Nefastious (31st October 2009)

  12. #8
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Problems with QSortFilterProxyModel

    terribly sorry sir... i didn't read your post...

    Now that I have tried the code:

    Qt Code:
    1. rows.append( ui->View->model()->mapToSource(index).row() )
    To copy to clipboard, switch view to plain text mode 
    ;

    my compiler tells me that model has no member named mapToSource...

    Again what am I doing wrong?

    And once again please accept my apologies for having failed to read your post, and, I must sincerely thank you for all of your wisdom, knowledge, support and patience.

  13. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with QSortFilterProxyModel

    Doesn't model() return a QAbstractItemModel, whereas you want a QAbstractProxyModel to get mapToSource? QAbstractProxyModel inherits from QAbstractItemModel, so...

    Qt Code:
    1. QAbstractProxyModel *q = dynamic_cast<QAbstractProxyModel*>(ui->View->model());
    2. if (q) q->mapToSource(...)
    3. else q isn't a valid pointer to a QAbstractProxyModel
    To copy to clipboard, switch view to plain text mode 

    Just a guess...

  14. The following user says thank you to squidge for this useful post:

    Nefastious (31st October 2009)

  15. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QSortFilterProxyModel

    my bad. You must cast ui->View->model() to QAbstractItemModel or it your be easier if you would store a pointer to the sort model as a member variable, e.g. m_sortModel.

    EDIT: I'm too late, and yes, fatjuicymole, you are right.
    Last edited by Lykurg; 31st October 2009 at 18:45. Reason: updated contents

  16. The following user says thank you to Lykurg for this useful post:

    Nefastious (31st October 2009)

  17. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problems with QSortFilterProxyModel

    Quote Originally Posted by fatjuicymole View Post
    Qt Code:
    1. QAbstractProxyModel *q = dynamic_cast<QAbstractProxyModel*>(ui->View->model());
    To copy to clipboard, switch view to plain text mode 
    Since the model includes QObject it is better to use qobject_cast since
    The qobject_cast() function behaves similarly to the standard C++ dynamic_cast(), with the advantages that it doesn't require RTTI support and it works across dynamic library boundaries.
    But in that case I would use static_cast.

  18. The following user says thank you to Lykurg for this useful post:

    Nefastious (31st October 2009)

Similar Threads

  1. Mac OSX OpenGL problems
    By tksharpless in forum Qt Programming
    Replies: 0
    Last Post: 23rd March 2009, 17:27
  2. problems creating toolbar...(do see the attachment)!
    By sumit in forum Qt Programming
    Replies: 15
    Last Post: 10th September 2008, 11:23
  3. flicker and wierd resize problems ...
    By momesana in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2008, 18:00
  4. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39

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.