From 4284b9d4b4e51107c682979e0fd202fd9d75f65e Mon Sep 17 00:00:00 2001 From: khamidou Date: Sat, 23 Jul 2011 09:38:39 +0200 Subject: [PATCH] added a delete function --- mainwindow.cpp | 25 ++++++++++++++++++++++++- mainwindow.h | 4 +++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 489d213..8d30932 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -42,18 +42,24 @@ void MainWindow::createActions() { newEntry = new QAction(QIcon("./icons/new.png"), tr("&New"), this); newEntry->setShortcuts(QKeySequence::New); - + newEntry->setStatusTip(tr("Create a new entry")); connect(newEntry, SIGNAL(triggered()), this, SLOT(insertRow())); + + deleteEntry = new QAction(QIcon("./icons/delete.png"), tr("&Delete"), this); + newEntry->setShortcuts(QKeySequence::Delete); + connect(deleteEntry, SIGNAL(triggered()), this, SLOT(deleteRows())); } void MainWindow::createToolbar() { toolBar = addToolBar(tr("New")); toolBar->addAction(newEntry); + toolBar->addAction(deleteEntry); } void MainWindow::insertRow() { + qDebug() << "paf"; QSqlQuery query; /* FIXME : don't hardcode field numbers */ @@ -61,6 +67,23 @@ void MainWindow::insertRow() model->select(); } +void MainWindow::deleteRows() { + + QItemSelectionModel *selectionModel = tableView->selectionModel(); + QModelIndexList indexes = selectionModel->selectedIndexes(); + + QModelIndex index; + + foreach(index, indexes) // loop through and remove them + { + int row = index.row(); + model->removeRows(row, 1, QModelIndex()); + } + + model->select(); + qDebug() << "ho"; +} + MainWindow::~MainWindow() { closeDb(); diff --git a/mainwindow.h b/mainwindow.h index cb6bda2..d1dd511 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -17,6 +17,7 @@ public: public slots: void insertRow(); + void deleteRows(); private: void setUpTableView(); @@ -26,7 +27,8 @@ private: QTabWidget *tabWidget; QTableView *tableView; QSqlRelationalTableModel *model; - QAction *newEntry; + QAction *newEntry; + QAction *deleteEntry; QToolBar *toolBar; }; -- 2.43.0