Skip to content
Permalink
Browse files

Make a custom delegate for the table editor

  • Loading branch information
nyalldawson committed Jan 14, 2020
1 parent 2117aa7 commit 60d5e22a05a521df2086485893978dadf98a5f35
@@ -8,6 +8,8 @@





class QgsTableEditorWidget : QTableWidget
{
%Docstring
@@ -51,6 +51,7 @@ QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )
mViewFrame->setContentsMargins( 0, 0, 0, 0 );

mTableWidget->setFocus();
mTableWidget->setTableContents( QgsTableContents() << ( QgsTableRow() << QgsTableCell() ) );

connect( mTableWidget, &QgsTableEditorWidget::tableChanged, this, [ = ]
{
@@ -21,6 +21,7 @@
#include <QKeyEvent>
#include <QHeaderView>
#include <QMenu>
#include <QLineEdit>

QgsTableEditorWidget::QgsTableEditorWidget( QWidget *parent )
: QTableWidget( parent )
@@ -118,6 +119,9 @@ QgsTableEditorWidget::QgsTableEditorWidget( QWidget *parent )
mHeaderMenu->popup( verticalHeader()->mapToGlobal( point ) );
} );


setItemDelegate( new QgsTableEditorDelegate( this ) );

connect( selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsTableEditorWidget::activeCellChanged );
}

@@ -870,3 +874,37 @@ void QgsTableEditorWidget::setSelectionColumnWidth( double width )
emit tableChanged();
}

/// @cond PRIVATE
QgsTableEditorDelegate::QgsTableEditorDelegate( QObject *parent )
: QStyledItemDelegate( parent )
{

}

QWidget *QgsTableEditorDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index ) const
{
QLineEdit *w = new QLineEdit( parent ); \
w->setText( index.model()->data( index, Qt::EditRole ).toString() );
return w;
}

void QgsTableEditorDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
{
QVariant value = index.model()->data( index, Qt::EditRole );
if ( QLineEdit *lineEdit = qobject_cast<QLineEdit * >( editor ) )
{
lineEdit->setText( value.toString() );
}
}

void QgsTableEditorDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
if ( QLineEdit *lineEdit = qobject_cast<QLineEdit * >( editor ) )
{
model->setData( index, lineEdit->text(), Qt::EditRole );
model->setData( index, lineEdit->text(), Qt::DisplayRole );
}
}


///@endcond
@@ -21,6 +21,27 @@
#include "qgis_gui.h"
#include "qgstablecell.h"
#include <QTableWidget>
#include <QStyledItemDelegate>


#ifndef SIP_RUN
///@cond PRIVATE
///
class QgsTableEditorDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
QgsTableEditorDelegate( QObject *parent );
protected:
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem & /*option*/, const QModelIndex &index ) const override;
void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;

};

///@endcond

#endif

/**
* \ingroup gui

0 comments on commit 60d5e22

Please sign in to comment.
You can’t perform that action at this time.