PDA

View Full Version : after sorting get the indexes right



KillGabio
26th January 2012, 15:36
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:


QSortFilterProxyModel *sort_filter = new QSortFilterProxyModel(this);
sort_filter->setSourceModel(model);
this->ui->table_busqueda->setModel (sort_filter);
sort_filter->sort (0);
this->ui->table_busqueda->setHorizontalHeader (//here should go the header of the filter ...);

thanks everyone

wysota
26th January 2012, 16:00
QSortFilterProxyModel doesn't remap the headers. If you want such behaviour, you need to subclass QSortFilterProxyModel and reimplement headerData().

KillGabio
26th January 2012, 18:44
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


QVariant MySortModelFilter::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
return QSortFilterProxyModel::headerData(section, orientation, role);
return section + 1;
}

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 :(

wysota
26th January 2012, 20:43
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.

KillGabio
27th January 2012, 01:00
super simple explanation thank you very much... i have another question though...

I have the next code:



void ExportData::on_quitar_clicked()
{
this->rows_selected = this->ui->table_agregados->selectionModel ()->selectedRows ();
foreach (QModelIndex index, rows_selected){
this->ui->table_agregados->removeRow (index.row ());
}
}

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!

wysota
27th January 2012, 09:42
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.

KillGabio
27th January 2012, 15:45
i added more rows and it happened exactly as u said only half is deleted...mmm i think you are the QtGod :D thanks for the info

greetings from Argentina!

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



void ExportData::on_quitar_clicked()
{
while (this->ui->table_agregados->selectionModel ()->hasSelection ()){
this->rows_selected = this->ui->table_agregados->selectionModel ()->selectedRows ();
foreach (QModelIndex index, rows_selected){
this->ui->table_agregados->removeRow (index.row ());
}
}
}


thanks wysota

wysota
27th January 2012, 16:39
This is very suboptimal. Why don't you do it the right way?

QModelIndexList selected = ui->table_agregados->selectionModel()->selectedRows();
for(int i=selected.count()-1;i>=0;i--) {
ui->table_agregados->removeRow(selected.at(i).row());
}

KillGabio
27th January 2012, 17:14
doneeee :D 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!