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.

Task-number: QTBUG-128434
Change-Id: Ie5cc38edfa27984d186467e3372b05987f78d14c
Reviewed-by: David Faure <david.faure@kdab.com>
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit 5a28fe86af30bd3293f49ad09ee9c044e06ccbe3)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Christian Ehrlicher 2024-09-01 19:53:12 +02:00 committed by Qt Cherry-pick Bot
parent 05c0ce01fb
commit 2987e96a6c
5 changed files with 7 additions and 1 deletions

View File

@ -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"

View File

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

View File

@ -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();

View File

@ -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();
}
}

View File

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