Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QListWidget::setEditTriggers(QAbstractItemView::AnyKeyPressed) not working

From the book I'm reading:

By default, QListWidget is read-only. If we wanted the user to edit the items, we could set the view's edit triggers using QAbstractItemView::setEditTriggers(); for example, a setting of QAbstractItemView::AnyKeyPressed means that the user can begin editing an item just by starting to type.

So, I call the function in my code:

ui->listWidget->setEditTriggers(QAbstractItemView::AnyKeyPressed);

But when I select an item and start typing, nothing happens.

like image 391
sashoalm Avatar asked Oct 27 '12 06:10

sashoalm


1 Answers

It turns out that the items themselves also have an editable flag, so after adding them I had to iterate all of them and set it. Now it's working.

// set the editable flag for each item
for (int ii = 0; ii < ui->listWidget->count(); ii++) {
    ui->listWidget->item(ii)->setFlags(ui->listWidget->item(ii)->flags() | Qt::ItemIsEditable);
}
// set the editable triggers for the list widget
ui->listWidget->setEditTriggers(QAbstractItemView::AnyKeyPressed);
like image 53
sashoalm Avatar answered Sep 17 '22 22:09

sashoalm