Congratulations to our 2022 Qt Champions!

[Solved] QListWidgetItem rename

  • I have a QListWidget that the user populates with QListWidgetItems. The user can double-click the list item to rename it. I would like to programmatically put the list item in that 'renaming state', like when you press F2 in Windows Explorer with a file selected. How can I accomplish this?

  • Well,

    if you want to reproduce Explorer behavior, you should set QAbstractItemView::SelectedClicked and QAbstractItemView::EditKeyPressed with setEditTriggers method.


  • First, make QListWidgetItems editable. You can do it by setting Qt::ItemIsEditable flag. Then connect listWidgets itemClicked signal to your slot that changes item state to edit. ( QListWidget::editItem( QListWidgetItem* ) ) Afaik, for F12, you have to install event filter to listwidget or create custom listwidget.

  • Thanks, t3chNo.
    It works flawlessly.

  • Antonio's solution is better.

  • Why?

  • QListWidget internally supports your needs. You don't need to code much. Code less, create more :)

  • t3chNo is right, this is the main philosophy :)

    Joking aside, I guess that everybody's right, the point of view is different.

    Mine is: "Ok, assume that the item is editable ... which are the input keys (mouse or keyboard) that are allowed to enter in edit mode??" ... and these should be set.

    t3chNo is: "You should tell me if the single item is editable or not".

    Of course, if the default flags() allows editing, mine is the straightforward solution. Otherwise, you should do both.


Log in to reply