Solved Deleting row from QTableWidget and from Sqlite database
-
@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.
-
@JonB
I can just upgrade Qt to v6 and see if it works -
@Risver
You can indeed, though my personal preference would be to take the latest Qt5.x release. I still see too many issues with Qt6, not to mention that I think they still have not ported all modules, depends what bits you will be using.However, my learned colleagues may disagree and advise you to go for Qt6, i don't know....