QTableView implementation I've put together my first table using Qt's QTableView and a custom data model. Currently, some data is populated on initialisation, and the user has the option of adding and deleting entries.
#include &quot;mainwindow.h&quot;
#include &quot;ui_mainwindow.h&quot;
#include &lt;iostream&gt;
using namespace std;
QList&lt;Contact&gt; contacts;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);

    QList&lt;QString&gt; contactNames;
    QList&lt;QString&gt; contactPhoneNums;

    contacts.append(Contact{4,&quot;Adam&quot;});
    contacts.append(Contact{5,&quot;James&quot;});
    contacts.append(Contact{2,&quot;Emily&quot;});
    contacts.append(Contact{1,&quot;Mark&quot;});

    // Create model:
    PhoneBookModel = new TestModel(this);

    // Connect model to table view:
    ui-&gt;tableView-&gt;setModel(PhoneBookModel);
    ui-&gt;tableView-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);
    ui-&gt;tableView-&gt;setSelectionMode( QAbstractItemView::SingleSelection );

    // Make table header visible and display table:
    ui-&gt;tableView-&gt;horizontalHeader()-&gt;setVisible(true);
    ui-&gt;tableView-&gt;show();

    //slots to signals

    connect(ui-&gt;tableView-&gt;selectionModel(), &amp;QItemSelectionModel::selectionChanged, this, &amp;MainWindow::foo);

}

void MainWindow::foo(){
    QItemSelectionModel *selected = ui-&gt;tableView-&gt;selectionModel();
    QModelIndexList rowList = selected-&gt;selectedRows();

    if (rowList.size() == 0){
        ui-&gt;label-&gt;setText(&quot;YAY&quot;);
        ui-&gt;tableView-&gt;selectRow(0);

    } else {
        ui-&gt;label-&gt;setText(contacts.at(rowList.at(0).row()).name);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

TestModel::TestModel(QObject *parent) : QAbstractTableModel(parent)
{
}

int TestModel::rowCount(const QModelIndex &amp;parent) const
{
    Q_UNUSED(parent);
    return contacts.length();
}

int TestModel::columnCount(const QModelIndex &amp;parent) const
{
    Q_UNUSED(parent);
    return 2;
}

QVariant TestModel::data(const QModelIndex &amp;index, int role) const
{
    if (!index.isValid() || role != Qt::DisplayRole) {
        return QVariant();
    }
    if (index.column() == 0) {
        return contacts[index.row()].id;
    } else if (index.column() == 1) {
        return contacts[index.row()].name;
    }
    return QVariant();
}

void TestModel::update(int row){

   QModelIndex i = this-&gt;index(row,0,QModelIndex());
   QModelIndex p = this-&gt;index(row,1,QModelIndex());

   emit dataChanged(i,p);
   emit layoutChanged();
}

QVariant TestModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole &amp;&amp; orientation == Qt::Horizontal) {
        if (section == 0) {
            return QString(&quot;Name&quot;);
        } else if (section == 1) {
            return QString(&quot;Phone&quot;);
        }
    }
    return QVariant();
}

void MainWindow::on_DeleteButton_clicked()
{
    QItemSelectionModel *selected = ui-&gt;tableView-&gt;selectionModel();
    QModelIndexList rowList = selected-&gt;selectedRows();


    for (int i = 0; i &lt; rowList.count(); i++)
    {

        contacts.removeAt(rowList.at(i).row());
        if (rowList.at(i).row() == contacts.size()){ //last element
            ui-&gt;tableView-&gt;selectRow(rowList.at(i).row()-1);

        } else {

        }

        PhoneBookModel-&gt;update(rowList.at(i).row());
        foo();

    }

}

void MainWindow::on_AddButton_clicked()
{
    int id = ui-&gt;id-&gt;text().toInt();
    QString name = ui-&gt;name-&gt;text();

    contacts.append(Contact{id,name});

    PhoneBookModel-&gt;update(contacts.size()-1);

}

It all works fine, although I worry that I may be using QTableView incorrectly and/or inefficiently. Any suggestions on how to improve my code while maintaining the same functionality would be appreciated.
I can't find anything wrong.If really give some suggestions, it would be the case of the function name 'on_AddButton_clicked' and 'on_DeleteButton_clicked'.It will be better to use 'addButton' and 'deleteButton'.