Qt5 QTreeView cannot make text persist from cell into the editor

156 Views Asked by At

I am working on a kiosk system, and am using virtual keyboard. Works fine for editing fields in my QTreeView, by extending a delegate and passing back my own local QLineEdit. I can type on the virtual keyboard and the QLineEdit is populated correctly, and taking focus from the cell calls the delegate's destroyEditor(), where I grab the text from the editor and update the cell item, so that when the QLineEdit disappears, I see the correct text.

I need a couple changes here. When I click on the item, I want to populate it with some starter text, and when I click away from the item and then click back on it, I don't want it to be blank but I want it to continue showing the text that was in the cell before the editor was applied.

In createEditor(), I have added the following changes:

QWidget* IconFileSystemRenameDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    m_editor->setParent(parent);
    const QFileSystemModel* model = static_cast<const QFileSystemModel*>(index.model());
    m_keyboard->focusThis(m_editor);

    // NOTE: Added this block:
    // if text exists in New Name cell, keep that for editing, otherwise start with original name as text
    QString plugThis = model->fileName(model->index(0,4,model->parent(index)));
    if (plugThis.length() == 0)
        plugThis = model->fileName(model->index(0,0,model->parent(index)));
    m_editor->setText(plugThis);
    QString plugged = m_editor->text();

    // set editor visible
    m_editor->setVisible(true);
    return m_editor;
}

Which basically amounts to checking if the cell in column 4 was changed and not empty (eventually I will need to also make sure it's not the starter text). If changed, then I want to take the content of that cell, update the editor, and the return it

But when I first click into edit mode for the cell, no starter text (the file name I'm trying to edit) is shown, and when I have added text to the editor and click out and then click back in, the editor doesn't show my text, even though in both cases I can debug in to see that the text has in fact been applied.

Seems like it's getting blanked when passed back to the Qt caller. I haven't yet found a way to get this working properly.

0

There are 0 best solutions below