PDA

View Full Version : Remove selected QListWidgetItem



xgoan
27th September 2006, 11:28
Hi, someone knows why I cant remove the selected item?


void NuevoDestinatario::on_pushButtonEliminarTelefono_c licked(){
if(Telefonos()->currentRow()!=-1){
if(QMessageBox::question(this,"","Desea eliminar el elemento seleccionado",
QMessageBox::Yes, QMessageBox::No)==QMessageBox::Yes){
delete Telefonos()->takeItem(Telefonos()->currentRow());
}
}
else QMessageBox::information(this,"","No hay elementos seleccionados");
}

Always currentRow() is -1. And Telefonos() returns QListWidget*

Thanks in advance

jpn
27th September 2006, 12:14
There's a difference between "selected item" and "current item". See this post (http://www.qtcentre.org/forum/f-qt-programming-2/t-what-is-currentitem-668.html#3).

xgoan
27th September 2006, 13:08
But


QMessageBox::information(this, "", QString::number(Telefonos()->selectedItems().count()));

always returns 0 (with item selected)

jpn
27th September 2006, 14:55
Works fine for me with both Qt 4.1.4 and 4.2.0-rc1.

xgoan
28th September 2006, 13:55
Your sample runs perfectly, and I rewrite my function to this

void NuevoDestinatario::EliminarTelefono_clicked(){
qDeleteAll(Telefonos()->selectedItems());
//QMessageBox::information(this, "", QString::number(Telefonos()->selectedItems().count()));
//if(Telefonos()->currentRow()!=-1){
//if(QMessageBox::question(this,"","Desea eliminar el elemento seleccionado",
//QMessageBox::Yes, QMessageBox::No)==QMessageBox::Yes){
//delete Telefonos()->takeItem(Telefonos()->currentRow());
//}
//}
//else QMessageBox::information(this,"","No hay elementos seleccionados");
}

and same behaviour, when I click the button the selected item is deselected and nothing is deleted

jpn
28th September 2006, 14:10
Have you any custom code which could cause the deselection of the selected items? Are you overriding or filtering any events?

xgoan
28th September 2006, 14:37
Have you any custom code which could cause the deselection of the selected items? Are you overriding or filtering any events?

Nope, only 4 buttons connected to functions to interact with the listWidget.

jpn
28th September 2006, 14:42
Heh, weird.. Have you adjusted list widget's selection mode or selection behaviour? Do the items really remain as selected in the GUI when you press the button?