QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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}
32
33void QgsTableWidgetBase::addButton_clicked()
34{
35 const QItemSelectionModel *select = tableView->selectionModel();
36 const int pos = select->hasSelection() ? select->selectedRows()[0].row() : 0;
37 QAbstractItemModel *model = tableView->model();
38 model->insertRows( pos, 1 );
39 const QModelIndex index = model->index( pos, 0 );
40 tableView->scrollTo( index );
41 tableView->edit( index );
42 tableView->selectRow( pos );
43}
44
45void QgsTableWidgetBase::removeButton_clicked()
46{
47 const QItemSelectionModel *select = tableView->selectionModel();
48 // The UI is configured to have single row selection.
49 if ( select->hasSelection() )
50 {
51 tableView->model()->removeRows( select->selectedRows()[0].row(), 1 );
52 }
53}
54
55void QgsTableWidgetBase::onSelectionChanged()
56{
57 removeButton->setEnabled( tableView->selectionModel()->hasSelection() );
58}
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.