QListView in IconMode with custom QStyledItemDelegate. Editor implementation not working.



  • Hello!

    I have overridden setEditorData, createEditor, updateEditoryGeometry, and setModelData and have my own editor (essentially just a QLineEdit) but I have 2 problems:

    • the commitData signal is getting emitted immediately after I call listView.edit(modelIndex);

    • the editor is not visibly getting painted on screen anywhere. I was thinking it might paint it at 0,0 of the edited items tile in the QListView but I don't see it.

    I am trying to make a new directory, have it render in the listview then have the focus directed to the editor's QLineEdit so that they can rename it. Then when the rename is done the directory is saved to my database and the view can refresh based on a QSqlQuery.

    Any help very very much appreciated.

    QWidget *GridItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        qDebug() << Q_FUNC_INFO;
    
        TileEditor *editor = new TileEditor(parent);
        //QLineEdit *editor = new QLineEdit(parent);
        connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
        return editor;
    }
    
    void GridItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        qDebug() << Q_FUNC_INFO;
    
        TileEditor *tileEditor = qobject_cast<TileEditor *>(editor);
        //QLineEdit *tileEditor = qobject_cast<QLineEdit *>(editor);
        //tileEditor->setText("YOYOYO");
        TileContent tc = qvariant_cast<TileContent>(index.data());
        tileEditor->setLine1Text(index.sibling(index.row(), 1).data().toString());
    
    }
    
    void GridItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
        qDebug() << Q_FUNC_INFO;
    
        TileEditor *tileEditor = qobject_cast<TileEditor *>(editor);
        model->setData(index.sibling(index.row(), 1), tileEditor->getLine1Text());
    }
    
    void GridItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
    {
        qDebug() << "Update geometry: " << option.rect;
        editor->setGeometry(option.rect);
    }
    
    
    void GridItemDelegate::commitAndCloseEditor()
    {
        qDebug() << Q_FUNC_INFO;
    
        TileEditor *tileEditor = qobject_cast<TileEditor *>(sender());
        emit doneEditing(tileEditor->getLine1Text());
        emit commitData(tileEditor);
        emit closeEditor(tileEditor);
    }```
    
    Thank you,
    k


  • Try to do this:

    QWidget *GridItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        qDebug() << Q_FUNC_INFO;
    if (!index.isvalid())
        return QStyledItemDelegate::createEditor(parent, option, index);
    
    return
        TileEditor *editor = new TileEditor(parent);
        //QLineEdit *editor = new QLineEdit(parent);
        connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
        return editor;
    }
    
    void GridItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        qDebug() << Q_FUNC_INFO;
        if (!index.isvalid())
            return QStyledItemDelegate::setEditorData(editor, index);
        
        TileEditor *tileEditor = qobject_cast<TileEditor *>(editor);
        //QLineEdit *tileEditor = qobject_cast<QLineEdit *>(editor);
        //tileEditor->setText("YOYOYO");
        TileContent tc = qvariant_cast<TileContent>(index.data());
        tileEditor->setLine1Text(index.sibling(index.row(), 1).data().toString());
    
    }
    
    void GridItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
        qDebug() << Q_FUNC_INFO;
        if (!index.isvalid())
            return QStyledItemDelegate::setModelData(editor, model, index);
        
        TileEditor *tileEditor = qobject_cast<TileEditor *>(editor);
        model->setData(index.sibling(index.row(), 1), tileEditor->getLine1Text());
    }
    
    void GridItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        qDebug() << "Update geometry: " << option.rect;
        if (!index.isvalid())
            return QStyledItemDelegate::updateEditorGeometry(editor, option, index);
        editor->setGeometry(option.rect);
    }
    
    
    void GridItemDelegate::commitAndCloseEditor()
    {
        qDebug() << Q_FUNC_INFO;
    
        TileEditor *tileEditor = qobject_cast<TileEditor *>(sender());
        emit doneEditing(tileEditor->getLine1Text());
        emit commitData(tileEditor);
        emit closeEditor(tileEditor);
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.