On Qt 4.6.1, when I connect the itemChanged signal from a QListWidget, I do not see the continual emitting behaviour upon every edit as discussed in
Qt - signal for when QListWidget row is edited?
For me it only emits itemChanged upon pressing 'Enter', in the same manner as the commitData solution.
QListWidget* Groups = new QListWidget();
Groups->setEditTriggers(QAbstractItemView::AllEditTriggers);
connect(Groups,SIGNAL(itemChanged(QListWidgetItem*)),this,SLOT(slot_itemchanged(QListWidgetItem*)));
connect(Groups->itemDelegate(), SIGNAL(commitData(QWidget*)), this, SLOT(slot_dataCommited(QWidget*)));
Groups->insertItem(0,"search");
Groups->item(0)->setFlags(Groups->item(0)->flags() | Qt::ItemIsEditable);
How do I get it to emit a signal upon every change / keypress during the editing of item(0) so that I can implement an auto-complete type functionality ?
The task of the autocomplete must be done in the editor through the delegate:
#include <QtWidgets>
class CompleteDelegate: public QStyledItemDelegate
{
public:
using QStyledItemDelegate::QStyledItemDelegate;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
QWidget *editor = QStyledItemDelegate::createEditor(parent, option, index);
if(QLineEdit *le = qobject_cast<QLineEdit *>(editor)){
QStringList wordList{"alpha", "omega", "omicron", "zeta"};
QCompleter *completer = new QCompleter(wordList, le);
//completer->setCompletionMode(QCompleter::InlineCompletion);
le->setCompleter(completer);
}
return editor;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QListWidget w;
CompleteDelegate *delegate = new CompleteDelegate(&w);
w.setItemDelegate(delegate);
w.setEditTriggers(QAbstractItemView::AllEditTriggers);
for(const QString & text: {"search", "find"}){
QListWidgetItem *it = new QListWidgetItem(text);
it->setFlags(it->flags()| Qt::ItemIsEditable);
w.addItem(it);
}
w.show();
return a.exec();
}