Solved Deleting row from QTableWidget and from Sqlite database
-
qDebug() << querymodel->removeRows(addressId, 1);
Please add
qDebug() << addressId
, we need to make sure what that is, just in case..... -
@JonB said in Deleting row from QTableWidget and from Sqlite database:
qDebug() << querymodel->removeRows(addressId, 1);
Please add
qDebug() << addressId
, we need to make sure what that is, just in case.....It is a variable to store the selected row, for example when i select the first row, it returns 0.
I checked this and its working fine -
@Risver
OK, so independent of your selectionquerymodel->removeRows(0, 1);
fails to remove the first row? We can then rule out anything to do with the UI or the selection. -
@JonB
Exactly. -
@JonB said in Deleting row from QTableWidget and from Sqlite database:
@Risver
OK, so independent of your selectionquerymodel->removeRows(0, 1);
fails to remove the first row? We can then rule out anything to do with the UI or the selection.Do you have something specific in mind ?
-
@Risver
Hang on. DoesQSqlQueryModel
allow any removing it all? It's a query, it won't/shouldn't do anything to the database for sure.[Yep, I'm sure,
QSqlQueryModel
is a read only model, you can't remove rows from it.]If you are wanting to alter the underlying the database table, I think you should be using
QSqlTableModel
instead?I leave this to you, or @Christian-Ehrlicher, now, as I'm done for the week :)
-
@JonB said in Deleting row from QTableWidget and from Sqlite database:
@Risver
Hang on. DoesQSqlQueryModel
allow any removing it all? It's a query, it won't/shouldn't do anything to the database for sure.[Yep, I'm sure,
QSqlQueryModel
is a read only model, you can't remove rows from it.]If you are wanting to alter the underlying the database table, I think you should be using
QSqlTableModel
instead?I leave this to you, or @Christian-Ehrlicher, now, as I'm done for the week :)
Okay, thank You, I will try to use QSqlTableModel.
-
I've changed the QSqlQueryModel into QSqlTableModel and everything is working !
Thank you all ! :)I have one more question - how can i refresh the table at the current time ?
Done it ! -
I have one more question - can i get data from column and replace these with other data ?
-
Hi,
Do you mean an entire column ?
-
@SGaist said in Deleting row from QTableWidget and from Sqlite database:
Hi,
Do you mean an entire column ?
Hi, yes i mean all the cells that are in the column
-
Yes, select the whole column and then use a loop to parse the selection.
-
@SGaist
Okay and can i replace the old data with new data ? -
@Risver
What "new data", from where? Do you mean something like over-pasting a selected column's data with other data in theQTreeView
UI? Do you mean updating all rows to change one column's data in the model and/or the SQL database? Programatically or by user interaction? Or what? -
@JonB said in Deleting row from QTableWidget and from Sqlite database:
@Risver
What "new data", from where? Do you mean something like over-pasting a selected column's data with other data in theQTreeView
UI? Do you mean updating all rows to change one column's data in the model and/or the SQL database? Programatically or by user interaction? Or what?I mean updating all rows to change one column's data in the model(not in SQL), I would like this to happen programatically.
The passwords in database are encrypted, I want them to be decrypted when I open the application.
-
@Risver
The you must loop over each row, updating the column:for (int row = 0; row < querymodel->rowCount(); row++) querymodel->setData(querymodel->index(row, someColumnNumber), someValue);
Is that what you meant?
-
@JonB said in Deleting row from QTableWidget and from Sqlite database:
@Risver
The you must loop over each row, updating the column:for (int row = 0; row < querymodel->rowCount(); row++) querymodel->setData(querymodel->index(row, someColumnNumber), someValue);
Is that what you meant?
Yes, thank you. But it is also changing the data in database.
-
@Risver
Of course. If you change aQSqlTableModel
's data it will end up updating the database. That's the point of it.You need to think out what you do and do not want to update, when and why.
-
@JonB
Now i'm usingui->tableView->model()->setData(ui->tableView->model()->index(i, 3), passdecoded);
But it's only work for first row.
for(int i = 0; i < table->rowCount(); i++) { QByteArray pass = ui->tableView->model()->data(ui->tableView->model()->index(i, 3)).toString().toUtf8(); pass = QByteArray::fromBase64(pass); QString passdecoded = decoding(pass); qDebug() << passdecoded; ui->tableView->model()->setData(ui->tableView->model()->index(i, 3), passdecoded); }
Now i see its also changing the data in database...
-
@Risver
Read https://doc.qt.io/qt-5/qsqltablemodel.html#submitAll, https://doc.qt.io/qt-5/qsqltablemodel.html#EditStrategy-enum and https://doc.qt.io/qt-5/qsqltablemodel.html#setEditStrategy, and check what yours currently is.