Results 1 to 6 of 6

Thread: can't get right data after sort

  1. #1
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question can't get right data after sort

    I realize that after the sorting process by clicking on the header in the tableView, making the data in the table to be inaccurate

    example

    1 | "Si a"
    5 | "Si e"
    2 | "Si b"

    After click table header and data sorted

    1 | "Si a"
    2 | "Si b" <- click this row
    5 | "Si e"

    I still get old data. Still 5 | "Si e", not 2 | "si b"

    Qt Code:
    1. model=new QSqlQueryModel();
    2. model->setQuery("select * from almari");
    3.  
    4. proxyModel->setSourceModel(model);
    5.  
    6. ui->tableView->setModel(proxyModel);
    7. ui->tableView->setSortingEnabled(TRUE);
    8.  
    9. connect(ui->tableView, SIGNAL(pressed(const QModelIndex &)), this, SLOT(clickForEdit(const QModelIndex &)));
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void Dialog::clickForEdit(const QModelIndex &index)
    2. {
    3. int row = index.row();
    4. qDebug() << " ID : " << model->record(row).value("id").toInt();
    5. qDebug() << " Nama : " << model->record(row).value("nama").toString();
    6. }
    To copy to clipboard, switch view to plain text mode 


    Sorry, my english is bad

  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: can't get right data after sort

    Not tested, but you have to convert the index of your sort model to your source model:

    Qt Code:
    1. int row = proxyModel->mapToSource(index).row(); // proxyModel must be a member variable!
    To copy to clipboard, switch view to plain text mode 

    EDIT: Or use ui->tableView->model()->mapToSource(index)...

  3. #3
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: can't get right data after sort

    Quote Originally Posted by wirasto View Post
    Qt Code:
    1. void Dialog::clickForEdit(const QModelIndex &index)
    2. {
    3. int row = index.row();
    4. qDebug() << " ID : " << model->record(row).value("id").toInt();
    5. qDebug() << " Nama : " << model->record(row).value("nama").toString();
    6. }
    To copy to clipboard, switch view to plain text mode 
    after the sort only proxy model's indexes changed so:
    Qt Code:
    1. model->record(row).value("id").toInt();
    To copy to clipboard, switch view to plain text mode 
    would return same value as long as you click on same row and sorting has nothing to do with this, because you have index from proxy model, taking it row in proxy model, and than taking value from source model at row which is valid only in proxy model.
    What you have to do is to map proxy index to source index:
    Qt Code:
    1. QModelIndex sourceIndex = proxyModel->mapToSource(index);
    To copy to clipboard, switch view to plain text mode 
    and then take the source row:
    Qt Code:
    1. int row = sourceIndex.row();
    To copy to clipboard, switch view to plain text mode 
    and now you can use the row to get proper data.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  4. #4
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Re: can't get right data after sort

    Ok, solved. Thank's

    But, how about use QSqlTableModel and sort enabled too.

    /home/wirasto/qt4/proxy/dialog.cpp:56: error: 'class QSqlTableModel' has no member named 'mapToSource'

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: can't get right data after sort

    Quote Originally Posted by wirasto View Post
    Ok, solved. Thank's

    But, how about use QSqlTableModel and sort enabled too.

    /home/wirasto/qt4/proxy/dialog.cpp:56: error: 'class QSqlTableModel' has no member named 'mapToSource'
    QSqlTableModel IS your source model and mapToSource is proxy model method so you have to use it on proxy model, and you get the QSqlTableModel's index;
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #6
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Thumbs up Re: can't get right data after sort

    OK. Solved.

    Thank's

Similar Threads

  1. data rate transfer is decreasing in TCP connection
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 16:15
  2. Best way to display lots of data fast
    By New2QT in forum Newbie
    Replies: 4
    Last Post: 16th October 2008, 22:46
  3. Replies: 7
    Last Post: 29th August 2008, 10:24
  4. Replies: 4
    Last Post: 19th October 2007, 19:47
  5. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53

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.