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:
Code:
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
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().
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
Code:
QVariant MySortModelFilter
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if (role != Qt::DisplayRole)
if (orientation == Qt::Horizontal)
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 :(
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.
Re: after sorting get the indexes right
super simple explanation thank you very much... i have another question though...
I have the next code:
Code:
void ExportData::on_quitar_clicked()
{
this->rows_selected = this->ui->table_agregados->selectionModel ()->selectedRows ();
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!
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.
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 :D thanks for the info
greetings from Argentina!
PS: i solved it by doing a while has more selected rows :)
Code:
void ExportData::on_quitar_clicked()
{
while (this->ui->table_agregados->selectionModel ()->hasSelection ()){
this->rows_selected = this->ui->table_agregados->selectionModel ()->selectedRows ();
this->ui->table_agregados->removeRow (index.row ());
}
}
}
thanks wysota
Re: after sorting get the indexes right
This is very suboptimal. Why don't you do it the right way?
Code:
QModelIndexList selected = ui->table_agregados->selectionModel()->selectedRows();
for(int i=selected.count()-1;i>=0;i--) {
ui->table_agregados->removeRow(selected.at(i).row());
}
Re: after sorting get the indexes right
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!