CodexBloom - Programming Q&A Platform

QML TextField not updating model data in Qt 6.4 when using custom delegate

πŸ‘€ Views: 81 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-13
qt qml listview model

I'm maintaining legacy code that I'm following best practices but I'm trying to configure This might be a silly question, but I'm having trouble with a custom delegate in a QML ListView where a TextField is not updating the model data as expected..... I've defined a ListView that uses a custom delegate to display items, but the changes I make in the TextField are not being reflected in the underlying model. Here's a simplified version of my code: ```qml ListView { id: listView model: myListModel delegate: Item { width: listView.width height: 50 TextField { id: textField text: model.display onTextChanged: { model.setData(index, textField.text); } } } } ``` My model is a simple QAbstractListModel that I implemented in C++. I have the `setData` method properly set up to handle updates, but when I type into the TextField, the model does not receive the new value. I tried adding `console.log` statements to check if `onTextChanged` is being triggered, and it is, but the model doesn’t update: ```cpp bool MyListModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (role == Qt::EditRole) { dataList[index.row()] = value.toString(); emit dataChanged(index, index, {role}); return true; } return false; } ``` I also ensured that I have the necessary roles defined in my model, specifically the `Qt::EditRole`, and the data is being returned correctly through `data()`. I'm using Qt 6.4. Any ideas on why my model isn't updating with the new text from the TextField? Is there something I'm missing in the binding or the signal-slot connection? I'm working on a service that needs to handle this. Thanks for taking the time to read this! Is this even possible? Is this even possible?