QListWidget::setEditTriggers(QAbstractItemView::AnyKeyPressed) not working
Asked Answered
M

1

7

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.

Madaras answered 27/10, 2012 at 6:26 Comment(0)
M
10

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);
Madaras answered 28/10, 2012 at 11:53 Comment(1)
If we are in QTreeView, self.model.setReadOnly(False) can solve this problem. It is struck me long time, make a note here.Chancellery

© 2022 - 2024 — McMap. All rights reserved.