Solved Deleting row from QTableWidget and from Sqlite database
-
Hi, I have a QTableWidget which is loading data from my Sqlite database. I need to make a delete selected row mechanism, but i don't know how to do this. Please help.
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QObject> #include <QMainWindow> #include <QSqlQueryModel> #include <QHeaderView> #include "signup.h" #include "login.h" #include "dbmanager.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void nextWindow(QString); private slots: void on_login_clicked(); void on_signup_clicked(); void on_remove_clicked(); void on_tableWidget_cellClicked(int row, int column); private: //Ui::MainWindow *ui; login * m_logWindow{nullptr}; signup * m_regWindow{nullptr}; QSqlQueryModel * querymodel; void readList(QString); QString Bufferbase; QSqlDatabase m_db; QSqlQuery qry; protected: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setWindowFlags(Qt::MSWindowsFixedSizeDialogHint); m_logWindow = new login(this); m_regWindow = new signup(this); connect(m_logWindow,&login::LoginSuccess, this, &MainWindow::nextWindow); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_login_clicked() { m_logWindow->show(); } void MainWindow::on_signup_clicked() { m_regWindow->show(); } void MainWindow::nextWindow(QString base) { ui->stackedWidget->setCurrentIndex(1); readList(base); Bufferbase = base; } void MainWindow::readList(QString base) { m_db.setDatabaseName("DiffPass.sqlite"); QSqlQuery qry(m_db); QString sBuffer = "SELECT * FROM "+base; qry.exec(sBuffer); ui->tableWidget->setColumnCount(3); QStringList labels; labels << "Url" << "E-mail" << "Password"; ui->tableWidget->setHorizontalHeaderLabels(labels); int rowCount = 0; while(qry.next()) { ui->tableWidget->insertRow(rowCount); QTableWidgetItem * url = new QTableWidgetItem; QTableWidgetItem * email = new QTableWidgetItem; QTableWidgetItem * password = new QTableWidgetItem; url->setText(qry.value(1).toString()); email->setText(qry.value(1).toString()); password->setText(qry.value(1).toString()); ui->tableWidget->setItem(rowCount, 0, url); ui->tableWidget->setItem(rowCount, 1, email); ui->tableWidget->setItem(rowCount, 2, password); rowCount++; } } void MainWindow::on_remove_clicked() { }
Thank you in advance!
-
@Risver Why don't you use QTableView with a proper model?
Back to your question: what exactly are you asking? To delete a row from a table in a SQL database you use https://www.w3schools.com/sql/sql_delete.asp . So, what is unclear?
-
@jsulm
I read that in QTableView you cannot delete the rows.
Yes, I know that i can use SQL query but i need to deleting the row after index. For example when i delete the 1 row, the second row will be the first with id = 2. -
@Risver said in Deleting row from QTableWidget and from Sqlite database:
I read that in QTableView you cannot delete the rows
Because that is the job of the model, not view.
See https://doc.qt.io/qt-5/qsqltablemodel.html#removeRows -
@jsulm
Okey i swapped the QTableWidget to QTableView. Now it looks like:#include "mainwindow.h" #include "ui_mainwindow.h" #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setWindowFlags(Qt::MSWindowsFixedSizeDialogHint); m_logWindow = new login(this); m_regWindow = new signup(this); connect(m_logWindow,&login::LoginSuccess, this, &MainWindow::nextWindow); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_login_clicked() { m_logWindow->show(); } void MainWindow::on_signup_clicked() { m_regWindow->show(); } void MainWindow::nextWindow(QString base) { ui->stackedWidget->setCurrentIndex(1); readList(base); Bufferbase = base; } void MainWindow::readList(QString base) { m_db.setDatabaseName("DiffPass.sqlite"); QSqlQuery qry(m_db); QString sBuffer = "SELECT * FROM "+base; qry.exec(sBuffer); querymodel = new QSqlQueryModel(); querymodel -> setQuery(sBuffer); ui -> tableView -> setModel(querymodel); } void MainWindow::on_remove_clicked() { }
And now how can i detect the selected row signal ?
-
@Risver said in Deleting row from QTableWidget and from Sqlite database:
And now how can i detect the selected row signal ?
What about https://doc.qt.io/qt-5/qtableview.html#selectionChanged ?
-
@jsulm
Okay and how can i connect it with the button_clicked signal? -
@Risver said in Deleting row from QTableWidget and from Sqlite database:
Okay and how can i connect it with the button_clicked signal?
You won't
When you react on the button clicked signal, get the current selection from the view and delete the selected rows in the model. -
@Christian-Ehrlicher said in Deleting row from QTableWidget and from Sqlite database:
@Risver said in Deleting row from QTableWidget and from Sqlite database:
Okay and how can i connect it with the button_clicked signal?
You won't
When you react on the button clicked signal, get the current selection from the view and delete the selected rows in the model.Something like this ?
void MainWindow::on_remove_clicked() { int selectedRow = ui->tableView->selectionModel()->currentIndex().row(); }
-
@Risver Yes.
-
@Christian-Ehrlicher
Now when i'm trying to implement QSqlTableModel::deleteRowFromTable the debugger says the deleteRowFromTable is a protected member of QSqlTableModel -
-
@jsulm
Could you give some example ? -
@Risver Example?
model->removeRows(row, 1);
-
@jsulm
Now i have error - reference to type 'const QModelIndex' could not bind to an rvalue of type 'int'.
I think i have to refer to QModelIndex but i don't know how. I have a QSqlQueryModel. Should i change it to QAbstractTableModel ? -
@Risver Please show how you're calling removeRows
And what Qt version do you use? -
void MainWindow::on_remove_clicked() { if (ui->tableView->selectionModel()->hasSelection()) { int addressId = ui->tableView->selectionModel()->currentIndex().row(); querymodel->removeRow(addressId, 1); qDebug() << addressId; } }
The QT version is 5.0.2
-
@Risver said in Deleting row from QTableWidget and from Sqlite database:
QT version is 5.0.2
Check the documentation for removeRows() in that Qt version.
Probably it has a different signature.Is there a reason why you're using such an ancient version?
-
@jsulm said in Deleting row from QTableWidget and from Sqlite database:
Is there a reason why you're using such an ancient version?
I just installed it a while time ago.
-
@Risver said in Deleting row from QTableWidget and from Sqlite database:
I just installed it a while time ago.
Before you spend any more time on this. Qt5 is already up to 5.15, unless you have some deep reason for wanting 5.0 I would uninstall and get such an up-to-date version.