Russian Qt Forum

Qt => Model-View (MV) => Тема начата: imironchik от Декабрь 16, 2011, 19:28



Название: Делегат для редактирования и QTextEdit
Отправлено: imironchik от Декабрь 16, 2011, 19:28
Добрый день.

Подскажите, пожалуйста, каким образом уловить завершение редактирования в QTextEdit? Это нужно для того, что сделать emit commitData()... Или может у Вас есть пример использования QTextEdit в качестве редактора в делегате?!


Название: Re: Делегат для редактирования и QTextEdit
Отправлено: andrew.k от Декабрь 16, 2011, 23:03
отлавливай потерю фокуса, например.


Название: Re: Делегат для редактирования и QTextEdit
Отправлено: imironchik от Декабрь 17, 2011, 08:37
Я отлавливаю клавиатурное событие. Только вот после всего у меня в модели не меняется значение. Есть еще один нюанс: я использую для сортировки QSortFilterProxyModel.

Код:
//
// TextEdit
//

//! Editor for the delegate.
class TextEdit
    :    public QTextEdit
{
    Q_OBJECT

signals:
    void editingFinished();

public:
    TextEdit( QWidget * parent = 0 )
        :    QTextEdit( parent )
    {
    }

protected:
    void keyPressEvent( QKeyEvent * e )
    {
        if( ( e->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Enter ) ||
            ( e->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Return ) )
                emit editingFinished();
        else
            QTextEdit::keyPressEvent( e );
    }
}; // TextEdit


//
// TextDelegate
//

//! Delegate for the text's fileds.
class TextDelegate
    :    public QStyledItemDelegate
{
    Q_OBJECT

signals:
    void dataChanged();

public:
    TextDelegate( QObject * parent = 0 )
        :    QStyledItemDelegate( parent )
    {
    }

    QWidget *
    createEditor( QWidget * parent, const QStyleOptionViewItem & option,
        const QModelIndex & index ) const
    {
        Q_UNUSED( option );
        Q_UNUSED( index );

        TextEdit * editor = new TextEdit( parent );
        connect( editor, SIGNAL( editingFinished() ),
            this, SLOT( slotCommitAndClose() ) );
        return editor;
    }

    void
    setEditorData( QWidget * editor, const QModelIndex & index ) const
    {
        TextEdit * e = qobject_cast< TextEdit* > ( editor );
        e->setPlainText( index.data().toString() );
    }

    void
    setModelData( QWidget * editor, QAbstractItemModel * model,
        const QModelIndex & index ) const
    {
        TextEdit * e = qobject_cast< TextEdit* > ( editor );
        model->setData( index, e->toPlainText() );
    }

    void updateEditorGeometry( QWidget * editor,
        const QStyleOptionViewItem & option, const QModelIndex & index ) const
    {
        Q_UNUSED( index );
        editor->setGeometry( option.rect );
    }

private slots:
    void slotCommitAndClose()
    {
        TextEdit * editor = qobject_cast< TextEdit* > ( sender() );
        emit commitData( editor );
        emit closeEditor( editor );
    }
}; // class TextDelegate


Название: Re: Делегат для редактирования и QTextEdit
Отправлено: imironchik от Декабрь 17, 2011, 18:27
Проблема была в не совсем верной установке данных в модели: я забыл указать Qt::DisplayRole. Поэтому данные не устанавливались. Т.е.

Код:
void
setModelData( QWidget * editor, QAbstractItemModel * model,
const QModelIndex & index ) const
{
TextEdit * e = qobject_cast< TextEdit* > ( editor );
model->setData( index, e->toPlainText(), Qt::DisplayRole );
}


Название: Re: Делегат для редактирования и QTextEdit
Отправлено: andrew.k от Декабрь 18, 2011, 10:10
только меня одного смущает слот slotCommitAndClose?