Results 1 to 9 of 9

Thread: after sorting get the indexes right

  1. #1
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default after sorting get the indexes right

    hi guys im here with a new basic question i couldnt find in the forum...

    after i sort my model with a QSortFilterProxyModel it seems that row number is attached to every row...as i want to display those numbers..hiding the horizontal index is not an option..so far what i`ve read is that i should use the horizontal data of the filter not the model...the thing is that i dont know how to do it ....

    here is what i ve done:

    Qt Code:
    1. sort_filter->setSourceModel(model);
    2. this->ui->table_busqueda->setModel (sort_filter);
    3. sort_filter->sort (0);
    4. this->ui->table_busqueda->setHorizontalHeader (//here should go the header of the filter ...);
    To copy to clipboard, switch view to plain text mode 

    thanks everyone

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

    Default Re: after sorting get the indexes right

    QSortFilterProxyModel doesn't remap the headers. If you want such behaviour, you need to subclass QSortFilterProxyModel and reimplement headerData().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    KillGabio (26th January 2012)

  4. #3
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Talking Re: after sorting get the indexes right

    Wysota and helpers,

    i did as you said..reimplement headerData ()....the thing is that i found the solution but i cant understand it, not even reading the documentation :P
    here is what i found and totally works
    Qt Code:
    1. QVariant MySortModelFilter::headerData(int section, Qt::Orientation orientation, int role) const
    2. {
    3. if (role != Qt::DisplayRole)
    4. return QVariant();
    5. if (orientation == Qt::Horizontal)
    6. return QSortFilterProxyModel::headerData(section, orientation, role);
    7. return section + 1;
    8. }
    To copy to clipboard, switch view to plain text mode 

    i know that the section is the row number and i understand that if we are talking about horizontal headers the process delegates it to the same function in proxymodel...

    what i dont understand is the +1 and the return of QVariant ();

    hope someone can bring some light to this burned head

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

    Default Re: after sorting get the indexes right

    QVariant() means that you retrurn an empty object for every role you don't want to handle and +1 means to start counting from 1 instead of 0.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    KillGabio (27th January 2012)

  7. #5
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: after sorting get the indexes right

    super simple explanation thank you very much... i have another question though...

    I have the next code:

    Qt Code:
    1. void ExportData::on_quitar_clicked()
    2. {
    3. this->rows_selected = this->ui->table_agregados->selectionModel ()->selectedRows ();
    4. foreach (QModelIndex index, rows_selected){
    5. this->ui->table_agregados->removeRow (index.row ());
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    everything works fine if i select only one row, but if i have multiple rows selected, the last one is not deleted…why is that happening? how would you solve it?

    thanks!

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

    Default Re: after sorting get the indexes right

    Are you sure only the last one is not deleted? I would expect that only half of what you select gets deleted (so if you select 20 rows, only 10 of them get deleted). This is because when you delete a row, all the remaining rows under it move up one row so if you delete row "1" then row "2" becomes row "1" and row "3" becomes row "2". Then when you delete row "2" then the row that used to be "3" gets deleted, omitting the original row "2". To avoid it, either delete starting from the end, use a list of QPersistantModelIndex instead of QModelIndex (not advised, slows down everything) or recalculate the indexes yourself. The first solution is the simplest one.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    KillGabio (27th January 2012)

  10. #7
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: after sorting get the indexes right

    i added more rows and it happened exactly as u said only half is deleted...mmm i think you are the QtGod thanks for the info

    greetings from Argentina!

    PS: i solved it by doing a while has more selected rows

    Qt Code:
    1. void ExportData::on_quitar_clicked()
    2. {
    3. while (this->ui->table_agregados->selectionModel ()->hasSelection ()){
    4. this->rows_selected = this->ui->table_agregados->selectionModel ()->selectedRows ();
    5. foreach (QModelIndex index, rows_selected){
    6. this->ui->table_agregados->removeRow (index.row ());
    7. }
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 


    thanks wysota

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

    Default Re: after sorting get the indexes right

    This is very suboptimal. Why don't you do it the right way?
    Qt Code:
    1. QModelIndexList selected = ui->table_agregados->selectionModel()->selectedRows();
    2. for(int i=selected.count()-1;i>=0;i--) {
    3. ui->table_agregados->removeRow(selected.at(i).row());
    4. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    KillGabio (27th January 2012)

  13. #9
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: after sorting get the indexes right

    doneeee actually im doing kind of a big personal project for my family and i dont have that much time (only 10 days left) so im not programming the way i want, i have big parts of replicated code because of that...guess when i have more time i ll finish this project properly...

    i ll let you know how this goes..thanks a lot!

Similar Threads

  1. QListWidget multiselection indexes
    By OverTheOCean in forum Newbie
    Replies: 2
    Last Post: 2nd June 2010, 06:13
  2. Count indexes in QListView
    By been_1990 in forum Qt Programming
    Replies: 5
    Last Post: 17th December 2009, 19:21
  3. How to mess QMdiArea indexes?
    By Raccoon29 in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2008, 21:46
  4. problem with indexes
    By MarkoSan in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2007, 14:55
  5. Getting indexes of selected items
    By gyre in forum Newbie
    Replies: 2
    Last Post: 20th November 2007, 19:23

Tags for this Thread

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.