2
\$\begingroup\$

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 "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
using namespace std;
QList<Contact> contacts;

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

    QList<QString> contactNames;
    QList<QString> contactPhoneNums;

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

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

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

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

    //slots to signals

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

}

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

    if (rowList.size() == 0){
        ui->label->setText("YAY");
        ui->tableView->selectRow(0);

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

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

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

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

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

QVariant TestModel::data(const QModelIndex &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->index(row,0,QModelIndex());
   QModelIndex p = this->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 && orientation == Qt::Horizontal) {
        if (section == 0) {
            return QString("Name");
        } else if (section == 1) {
            return QString("Phone");
        }
    }
    return QVariant();
}

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


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

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

        } else {

        }

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

    }

}

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

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

    PhoneBookModel->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.

\$\endgroup\$
1
\$\begingroup\$

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'.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ It will be better to use 'addButton' and 'deleteButton' No it would not, cause it would break the automatic signal connection. \$\endgroup\$ – scopchanov Jul 23 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.