From 5a28fe86af30bd3293f49ad09ee9c044e06ccbe3 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sun, 1 Sep 2024 19:53:12 +0200 Subject: [PATCH] Examples/Sql/Masterdetail: Fix deleting rows Completely deleting a row in a QSqlTableModel needs a call to select() afterwards, otherwise a blank column will stay according the documentation of QSqlTableModel::removeRows() Also add albumdetails.xml to the resource file to make sure it's found during runtime. Pick-to: 6.8 Task-number: QTBUG-128434 Change-Id: Ie5cc38edfa27984d186467e3372b05987f78d14c Reviewed-by: David Faure Reviewed-by: Axel Spoerl --- examples/sql/masterdetail/CMakeLists.txt | 1 + examples/sql/masterdetail/dialog.cpp | 1 + examples/sql/masterdetail/main.cpp | 2 +- examples/sql/masterdetail/mainwindow.cpp | 3 +++ examples/sql/masterdetail/masterdetail.qrc | 1 + 5 files changed, 7 insertions(+), 1 deletion(-) diff --git a/examples/sql/masterdetail/CMakeLists.txt b/examples/sql/masterdetail/CMakeLists.txt index 558bf70ee2e..454aae1b7d5 100644 --- a/examples/sql/masterdetail/CMakeLists.txt +++ b/examples/sql/masterdetail/CMakeLists.txt @@ -32,6 +32,7 @@ target_link_libraries(masterdetail PRIVATE set(masterdetail_resource_files "images/icon.png" "images/image.png" + "albumdetails.xml" ) qt_add_resources(masterdetail "masterdetail" diff --git a/examples/sql/masterdetail/dialog.cpp b/examples/sql/masterdetail/dialog.cpp index 0b8ed8a75b6..e1d64090e9b 100644 --- a/examples/sql/masterdetail/dialog.cpp +++ b/examples/sql/masterdetail/dialog.cpp @@ -173,6 +173,7 @@ void Dialog::increaseAlbumCount(const QModelIndex &artistIndex) int albumCount = albumCountIndex.data().toInt(); artistModel->setData(albumCountIndex, QVariant(albumCount + 1)); + artistModel->submitAll(); } diff --git a/examples/sql/masterdetail/main.cpp b/examples/sql/masterdetail/main.cpp index 50d99cf24e8..15a070bd67e 100644 --- a/examples/sql/masterdetail/main.cpp +++ b/examples/sql/masterdetail/main.cpp @@ -14,7 +14,7 @@ int main(int argc, char *argv[]) if (!createConnection()) return EXIT_FAILURE; - QFile albumDetails("albumdetails.xml"); + QFile albumDetails(":/albumdetails.xml"); MainWindow window("artists", "albums", &albumDetails); window.show(); return app.exec(); diff --git a/examples/sql/masterdetail/mainwindow.cpp b/examples/sql/masterdetail/mainwindow.cpp index 020a149b22f..f8b198eebc2 100644 --- a/examples/sql/masterdetail/mainwindow.cpp +++ b/examples/sql/masterdetail/mainwindow.cpp @@ -202,6 +202,8 @@ void MainWindow::removeAlbumFromFile(int id) void MainWindow::removeAlbumFromDatabase(const QModelIndex &index) { model->removeRow(index.row()); + // to avoid a blank row, see QSqlTableModel::removeRows() + model->select(); } void MainWindow::decreaseAlbumCount(const QModelIndex &artistIndex) @@ -217,6 +219,7 @@ void MainWindow::decreaseAlbumCount(const QModelIndex &artistIndex) showImageLabel(); } else { artists->setData(albumCountIndex, QVariant(albumCount - 1)); + artists->submitAll(); } } diff --git a/examples/sql/masterdetail/masterdetail.qrc b/examples/sql/masterdetail/masterdetail.qrc index 717c2eb945d..59059052000 100644 --- a/examples/sql/masterdetail/masterdetail.qrc +++ b/examples/sql/masterdetail/masterdetail.qrc @@ -2,5 +2,6 @@ images/icon.png images/image.png + albumdetails.xml