QListWidget and item edit event

QListWidget control allow, once properly configured, to edit items by, usually, double clicking over it. However if you want be informed when the user finished to edit item value for make your personal check there is no signals dedicated to this type of event. This because the line edit control created on the fly is delegated to another Qt object.



Anyway is quite easy to reach such result in the following way. First you need to enable the item edit feature by set the required flag as below (pItem is a QListWidgetItem object):

pItem->setFlags(pItem->flags() | Qt::ItemIsEditable);

If you want to connected the closeEditor signal to your slot function you need to use the following call (MyListWidget is a QListWidget object):

connect(MyListWidget->itemDelegate(), SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)), this, SLOT(ListWidgetEditEnd(QWidget*, QAbstractItemDelegate::EndEditHint)));

The slot function will be defined as follow:

void ListWidgetEditEnd(QWidget *editor, QAbstractItemDelegate::EndEditHint hint);

This function will be called once the user finish to edit the item. You can get the new string value inserted by the user by simply cast the first param as follow:

NewValue = reinterpret_cast<QLineEdit*>(editor)->text();

As you can note this function return the new item value but doesn't help you in know which item has been modified. For get this info you need to suppose the item just edited will be also a selected item and, in consequence of this, use the corresponding methods from QListWidget object.

Comments

Popular posts from this blog

Access GPIO from Linux user space

Android: adb push and read-only file system error

Tree in SQL database: The Nested Set Model