QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgstablewidgetbase.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstablewidgetbase.cpp
3 --------------------------------------
4 Date : 08.2016
5 Copyright : (C) 2016 Patrick Valsecchi
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgstablewidgetbase.h"
17
19 : QWidget( parent )
20{
21 setupUi( this );
22 connect( addButton, &QToolButton::clicked, this, &QgsTableWidgetBase::addButton_clicked );
23 connect( removeButton, &QToolButton::clicked, this, &QgsTableWidgetBase::removeButton_clicked );
24}
25
26void QgsTableWidgetBase::init( QAbstractTableModel *model )
27{
28 tableView->setModel( model );
29 connect( tableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsTableWidgetBase::onSelectionChanged );
30 connect( model, &QAbstractItemModel::dataChanged, this, &QgsTableWidgetBase::valueChanged );
31 connect( model, &QAbstractItemModel::rowsRemoved, this, &QgsTableWidgetBase::valueChanged );
32 connect( model, &QAbstractItemModel::rowsInserted, this, &QgsTableWidgetBase::valueChanged );
33}
34
35void QgsTableWidgetBase::addButton_clicked()
36{
37 const QItemSelectionModel *select = tableView->selectionModel();
38 const int pos = select->hasSelection() ? select->selectedRows()[0].row() : 0;
39 QAbstractItemModel *model = tableView->model();
40 model->insertRows( pos, 1 );
41 const QModelIndex index = model->index( pos, 0 );
42 tableView->scrollTo( index );
43 tableView->edit( index );
44 tableView->selectRow( pos );
45}
46
47void QgsTableWidgetBase::removeButton_clicked()
48{
49 const QItemSelectionModel *select = tableView->selectionModel();
50 // The UI is configured to have single row selection.
51 if ( select->hasSelection() )
52 {
53 tableView->model()->removeRows( select->selectedRows()[0].row(), 1 );
54 }
55}
56
57void QgsTableWidgetBase::onSelectionChanged()
58{
59 removeButton->setEnabled( tableView->selectionModel()->hasSelection() );
60}
void init(QAbstractTableModel *model)
Initialize the table with the given model.
void valueChanged()
Emitted each time a key or a value is changed.
QgsTableWidgetBase(QWidget *parent)
Constructor.