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 <david.faure@kdab.com>
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Christian Ehrlicher 2024-09-01 19:53:12 +02:00
parent 48753a03de
commit 5a28fe86af
5 changed files with 7 additions and 1 deletions

View File

@ -32,6 +32,7 @@ target_link_libraries(masterdetail PRIVATE
set(masterdetail_resource_files set(masterdetail_resource_files
"images/icon.png" "images/icon.png"
"images/image.png" "images/image.png"
"albumdetails.xml"
) )
qt_add_resources(masterdetail "masterdetail" qt_add_resources(masterdetail "masterdetail"

View File

@ -173,6 +173,7 @@ void Dialog::increaseAlbumCount(const QModelIndex &artistIndex)
int albumCount = albumCountIndex.data().toInt(); int albumCount = albumCountIndex.data().toInt();
artistModel->setData(albumCountIndex, QVariant(albumCount + 1)); artistModel->setData(albumCountIndex, QVariant(albumCount + 1));
artistModel->submitAll();
} }

View File

@ -14,7 +14,7 @@ int main(int argc, char *argv[])
if (!createConnection()) if (!createConnection())
return EXIT_FAILURE; return EXIT_FAILURE;
QFile albumDetails("albumdetails.xml"); QFile albumDetails(":/albumdetails.xml");
MainWindow window("artists", "albums", &albumDetails); MainWindow window("artists", "albums", &albumDetails);
window.show(); window.show();
return app.exec(); return app.exec();

View File

@ -202,6 +202,8 @@ void MainWindow::removeAlbumFromFile(int id)
void MainWindow::removeAlbumFromDatabase(const QModelIndex &index) void MainWindow::removeAlbumFromDatabase(const QModelIndex &index)
{ {
model->removeRow(index.row()); model->removeRow(index.row());
// to avoid a blank row, see QSqlTableModel::removeRows()
model->select();
} }
void MainWindow::decreaseAlbumCount(const QModelIndex &artistIndex) void MainWindow::decreaseAlbumCount(const QModelIndex &artistIndex)
@ -217,6 +219,7 @@ void MainWindow::decreaseAlbumCount(const QModelIndex &artistIndex)
showImageLabel(); showImageLabel();
} else { } else {
artists->setData(albumCountIndex, QVariant(albumCount - 1)); artists->setData(albumCountIndex, QVariant(albumCount - 1));
artists->submitAll();
} }
} }

View File

@ -2,5 +2,6 @@
<qresource> <qresource>
<file>images/icon.png</file> <file>images/icon.png</file>
<file>images/image.png</file> <file>images/image.png</file>
<file>albumdetails.xml</file>
</qresource> </qresource>
</RCC> </RCC>