Skip to content

Commit

Permalink
Make a custom delegate for the table editor
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 14, 2020
1 parent 2117aa7 commit 60d5e22
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
Expand Up @@ -8,6 +8,8 @@





class QgsTableEditorWidget : QTableWidget
{
%Docstring
Expand Down
1 change: 1 addition & 0 deletions src/gui/tableeditor/qgstableeditordialog.cpp
Expand Up @@ -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, [ = ]
{
Expand Down
38 changes: 38 additions & 0 deletions src/gui/tableeditor/qgstableeditorwidget.cpp
Expand Up @@ -21,6 +21,7 @@
#include <QKeyEvent>
#include <QHeaderView>
#include <QMenu>
#include <QLineEdit>

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


setItemDelegate( new QgsTableEditorDelegate( this ) );

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

Expand Down Expand Up @@ -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 changes: 21 additions & 0 deletions src/gui/tableeditor/qgstableeditorwidget.h
Expand Up @@ -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
Expand Down

0 comments on commit 60d5e22

Please sign in to comment.