diff options
Diffstat (limited to 'examples/sql/masterdetail')
-rw-r--r-- | examples/sql/masterdetail/CMakeLists.txt | 51 | ||||
-rw-r--r-- | examples/sql/masterdetail/database.h | 51 | ||||
-rw-r--r-- | examples/sql/masterdetail/dialog.cpp | 90 | ||||
-rw-r--r-- | examples/sql/masterdetail/dialog.h | 78 | ||||
-rw-r--r-- | examples/sql/masterdetail/main.cpp | 55 | ||||
-rw-r--r-- | examples/sql/masterdetail/mainwindow.cpp | 112 | ||||
-rw-r--r-- | examples/sql/masterdetail/mainwindow.h | 80 |
7 files changed, 125 insertions, 392 deletions
diff --git a/examples/sql/masterdetail/CMakeLists.txt b/examples/sql/masterdetail/CMakeLists.txt index 0e26401450..558bf70ee2 100644 --- a/examples/sql/masterdetail/CMakeLists.txt +++ b/examples/sql/masterdetail/CMakeLists.txt @@ -1,25 +1,12 @@ -# Generated from masterdetail.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(masterdetail LANGUAGES CXX) -set(CMAKE_INCLUDE_CURRENT_DIR ON) +find_package(Qt6 REQUIRED COMPONENTS Core Gui Sql Widgets Xml) -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/sql/masterdetail") - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Sql) -find_package(Qt6 COMPONENTS Widgets) -find_package(Qt6 COMPONENTS Xml) +qt_standard_project_setup() qt_add_executable(masterdetail database.h @@ -27,18 +14,19 @@ qt_add_executable(masterdetail main.cpp mainwindow.cpp mainwindow.h ) + set_target_properties(masterdetail PROPERTIES WIN32_EXECUTABLE TRUE MACOSX_BUNDLE TRUE ) -target_link_libraries(masterdetail PUBLIC - Qt::Core - Qt::Gui - Qt::Sql - Qt::Widgets - Qt::Xml -) +target_link_libraries(masterdetail PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Sql + Qt6::Widgets + Qt6::Xml +) # Resources: set(masterdetail_resource_files @@ -46,7 +34,7 @@ set(masterdetail_resource_files "images/image.png" ) -qt6_add_resources(masterdetail "masterdetail" +qt_add_resources(masterdetail "masterdetail" PREFIX "/" FILES @@ -54,7 +42,14 @@ qt6_add_resources(masterdetail "masterdetail" ) install(TARGETS masterdetail - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET masterdetail + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/sql/masterdetail/database.h b/examples/sql/masterdetail/database.h index 9cbca6f749..ed6d32b558 100644 --- a/examples/sql/masterdetail/database.h +++ b/examples/sql/masterdetail/database.h @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef DATABASE_H #define DATABASE_H diff --git a/examples/sql/masterdetail/dialog.cpp b/examples/sql/masterdetail/dialog.cpp index 7777929dfd..0b8ed8a75b 100644 --- a/examples/sql/masterdetail/dialog.cpp +++ b/examples/sql/masterdetail/dialog.cpp @@ -1,59 +1,25 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "dialog.h" -int uniqueAlbumId; -int uniqueArtistId; - -Dialog::Dialog(QSqlRelationalTableModel *albums, QDomDocument details, +#include <QDialogButtonBox> +#include <QDate> +#include <QDomDocument> +#include <QGroupBox> +#include <QLabel> +#include <QLineEdit> +#include <QMessageBox> +#include <QPushButton> +#include <QSpinBox> +#include <QSqlField> +#include <QSqlRelationalTableModel> +#include <QSqlRecord> +#include <QVBoxLayout> + +int Dialog::s_artistId = 0; +int Dialog::s_albumId = 0; +Dialog::Dialog(QSqlRelationalTableModel *albums, const QDomDocument &details, QFile *output, QWidget *parent) : QDialog(parent) { @@ -72,6 +38,12 @@ Dialog::Dialog(QSqlRelationalTableModel *albums, QDomDocument details, setWindowTitle(tr("Add Album")); } +void Dialog::setInitialAlbumAndArtistId(int albumId, int artistId) +{ + s_albumId = albumId; + s_artistId = artistId; +} + void Dialog::submit() { QString artist = artistEditor->text(); @@ -192,7 +164,7 @@ void Dialog::addTracks(int albumId, const QStringList &tracks) */ } -void Dialog::increaseAlbumCount(QModelIndex artistIndex) +void Dialog::increaseAlbumCount(const QModelIndex &artistIndex) { QSqlTableModel *artistModel = model->relationModel(2); @@ -266,7 +238,7 @@ QDialogButtonBox *Dialog::createButtons() return buttonBox; } -QModelIndex Dialog::indexOfArtist(const QString &artist) +QModelIndex Dialog::indexOfArtist(const QString &artist) const { QSqlTableModel *artistModel = model->relationModel(2); @@ -281,12 +253,12 @@ QModelIndex Dialog::indexOfArtist(const QString &artist) int Dialog::generateArtistId() { - uniqueArtistId += 1; - return uniqueArtistId; + s_artistId += 1; + return s_artistId; } int Dialog::generateAlbumId() { - uniqueAlbumId += 1; - return uniqueAlbumId; + s_albumId += 1; + return s_albumId; } diff --git a/examples/sql/masterdetail/dialog.h b/examples/sql/masterdetail/dialog.h index 31c4d8552c..2a4f5a0b8e 100644 --- a/examples/sql/masterdetail/dialog.h +++ b/examples/sql/masterdetail/dialog.h @@ -1,67 +1,28 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef DIALOG_H #define DIALOG_H -#include <QtWidgets> -#include <QtSql> -#include <QtXml> +#include <QDialog> +#include <QDomDocument> + +QT_FORWARD_DECLARE_CLASS(QDialogButtonBox) +QT_FORWARD_DECLARE_CLASS(QFile) +QT_FORWARD_DECLARE_CLASS(QGroupBox) +QT_FORWARD_DECLARE_CLASS(QLineEdit) +QT_FORWARD_DECLARE_CLASS(QModelIndex) +QT_FORWARD_DECLARE_CLASS(QSpinBox) +QT_FORWARD_DECLARE_CLASS(QSqlRelationalTableModel) class Dialog : public QDialog { Q_OBJECT public: - Dialog(QSqlRelationalTableModel *albums, QDomDocument details, + Dialog(QSqlRelationalTableModel *albums, const QDomDocument &details, QFile *output, QWidget *parent = nullptr); + static void setInitialAlbumAndArtistId(int albumId, int artistId); private slots: void revert(); @@ -74,10 +35,8 @@ private: QDialogButtonBox *createButtons(); QGroupBox *createInputWidgets(); int findArtistId(const QString &artist); - static int generateAlbumId(); - static int generateArtistId(); - void increaseAlbumCount(QModelIndex artistIndex); - QModelIndex indexOfArtist(const QString &artist); + void increaseAlbumCount(const QModelIndex &artistIndex); + QModelIndex indexOfArtist(const QString &artist) const; QSqlRelationalTableModel *model; QDomDocument albumDetails; @@ -87,6 +46,11 @@ private: QLineEdit *titleEditor; QSpinBox *yearEditor; QLineEdit *tracksEditor; + + static int generateAlbumId(); + static int generateArtistId(); + static int s_artistId; + static int s_albumId; }; #endif diff --git a/examples/sql/masterdetail/main.cpp b/examples/sql/masterdetail/main.cpp index 9a4172e55f..50d99cf24e 100644 --- a/examples/sql/masterdetail/main.cpp +++ b/examples/sql/masterdetail/main.cpp @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "database.h" #include "mainwindow.h" @@ -54,12 +7,8 @@ #include <QApplication> #include <QFile> -#include <stdlib.h> - int main(int argc, char *argv[]) { - Q_INIT_RESOURCE(masterdetail); - QApplication app(argc, argv); if (!createConnection()) diff --git a/examples/sql/masterdetail/mainwindow.cpp b/examples/sql/masterdetail/mainwindow.cpp index 11f123372c..020a149b22 100644 --- a/examples/sql/masterdetail/mainwindow.cpp +++ b/examples/sql/masterdetail/mainwindow.cpp @@ -1,62 +1,23 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "mainwindow.h" #include "dialog.h" -#include <QtWidgets> -#include <QtSql> -#include <QtXml> - -extern int uniqueAlbumId; -extern int uniqueArtistId; +#include <QApplication> +#include <QComboBox> +#include <QHeaderView> +#include <QFile> +#include <QGridLayout> +#include <QGroupBox> +#include <QLabel> +#include <QListWidget> +#include <QMenu> +#include <QMenuBar> +#include <QMessageBox> +#include <QSqlRecord> +#include <QSqlRelationalTableModel> +#include <QTableView> MainWindow::MainWindow(const QString &artistTable, const QString &albumTable, QFile *albumDetails, QWidget *parent) @@ -75,13 +36,12 @@ MainWindow::MainWindow(const QString &artistTable, const QString &albumTable, QGroupBox *details = createDetailsGroupBox(); artistView->setCurrentIndex(0); - uniqueAlbumId = model->rowCount(); - uniqueArtistId = artistView->count(); + Dialog::setInitialAlbumAndArtistId(model->rowCount(), artistView->count()); connect(model, &QSqlRelationalTableModel::rowsInserted, - this, &MainWindow::updateHeader); + this, &MainWindow::adjustHeader); connect(model, &QSqlRelationalTableModel::rowsRemoved, - this, &MainWindow::updateHeader); + this, &MainWindow::adjustHeader); QGridLayout *layout = new QGridLayout; layout->addWidget(artists, 0, 0); @@ -114,7 +74,7 @@ void MainWindow::changeArtist(int row) } } -void MainWindow::showArtistProfile(QModelIndex index) +void MainWindow::showArtistProfile(const QModelIndex &index) { QSqlRecord record = model->relationModel(2)->record(index.row()); @@ -131,7 +91,7 @@ void MainWindow::showArtistProfile(QModelIndex index) imageLabel->hide(); } -void MainWindow::showAlbumDetails(QModelIndex index) +void MainWindow::showAlbumDetails(const QModelIndex &index) { QSqlRecord record = model->record(index.row()); @@ -160,14 +120,11 @@ void MainWindow::getTrackList(QDomNode album) { trackList->clear(); - QDomNodeList tracks = album.childNodes(); - QDomNode track; - QString trackNumber; - + const QDomNodeList tracks = album.childNodes(); for (int i = 0; i < tracks.count(); ++i) { - track = tracks.item(i); - trackNumber = track.toElement().attribute("number"); + const QDomNode track = tracks.item(i); + const QString trackNumber = track.toElement().attribute("number"); QListWidgetItem *item = new QListWidgetItem(trackList); item->setText(trackNumber + ": " + track.toElement().text()); @@ -179,7 +136,7 @@ void MainWindow::addAlbum() Dialog *dialog = new Dialog(model, albumData, file, this); int accepted = dialog->exec(); - if (accepted == 1) { + if (accepted == QDialog::Accepted) { int lastRow = model->rowCount() - 1; albumView->selectRow(lastRow); albumView->scrollToBottom(); @@ -189,10 +146,10 @@ void MainWindow::addAlbum() void MainWindow::deleteAlbum() { - QModelIndexList selection = albumView->selectionModel()->selectedRows(0); + const QModelIndexList selection = albumView->selectionModel()->selectedRows(0); if (!selection.empty()) { - QModelIndex idIndex = selection.at(0); + const QModelIndex &idIndex = selection.at(0); int id = idIndex.data().toInt(); QString title = idIndex.sibling(idIndex.row(), 1).data().toString(); QString artist = idIndex.sibling(idIndex.row(), 2).data().toString(); @@ -219,9 +176,7 @@ void MainWindow::deleteAlbum() void MainWindow::removeAlbumFromFile(int id) { - QDomNodeList albums = albumData.elementsByTagName("album"); - for (int i = 0; i < albums.count(); ++i) { QDomNode node = albums.item(i); if (node.toElement().attribute("id").toInt() == id) { @@ -244,12 +199,12 @@ void MainWindow::removeAlbumFromFile(int id) */ } -void MainWindow::removeAlbumFromDatabase(QModelIndex index) +void MainWindow::removeAlbumFromDatabase(const QModelIndex &index) { model->removeRow(index.row()); } -void MainWindow::decreaseAlbumCount(QModelIndex artistIndex) +void MainWindow::decreaseAlbumCount(const QModelIndex &artistIndex) { int row = artistIndex.row(); QModelIndex albumCountIndex = artistIndex.sibling(row, 2); @@ -388,7 +343,7 @@ void MainWindow::createMenuBar() connect(deleteAction, &QAction::triggered, this, &MainWindow::deleteAlbum); connect(quitAction, &QAction::triggered, - this, &MainWindow::close); + qApp, &QCoreApplication::quit); connect(aboutAction, &QAction::triggered, this, &MainWindow::about); connect(aboutQtAction, &QAction::triggered, @@ -405,7 +360,7 @@ void MainWindow::showImageLabel() imageLabel->show(); } -QModelIndex MainWindow::indexOfArtist(const QString &artist) +QModelIndex MainWindow::indexOfArtist(const QString &artist) const { QSqlTableModel *artistModel = model->relationModel(2); @@ -417,11 +372,6 @@ QModelIndex MainWindow::indexOfArtist(const QString &artist) return QModelIndex(); } -void MainWindow::updateHeader(QModelIndex, int, int) -{ - adjustHeader(); -} - void MainWindow::adjustHeader() { albumView->hideColumn(0); diff --git a/examples/sql/masterdetail/mainwindow.h b/examples/sql/masterdetail/mainwindow.h index dc6d8d1613..cc2a5c9f59 100644 --- a/examples/sql/masterdetail/mainwindow.h +++ b/examples/sql/masterdetail/mainwindow.h @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef MAINWINDOW_H #define MAINWINDOW_H @@ -55,15 +8,13 @@ #include <QMainWindow> #include <QModelIndex> -QT_BEGIN_NAMESPACE -class QComboBox; -class QFile; -class QGroupBox; -class QLabel; -class QListWidget; -class QSqlRelationalTableModel; -class QTableView; -QT_END_NAMESPACE +QT_FORWARD_DECLARE_CLASS(QComboBox) +QT_FORWARD_DECLARE_CLASS(QFile) +QT_FORWARD_DECLARE_CLASS(QGroupBox) +QT_FORWARD_DECLARE_CLASS(QLabel) +QT_FORWARD_DECLARE_CLASS(QListWidget) +QT_FORWARD_DECLARE_CLASS(QSqlRelationalTableModel) +QT_FORWARD_DECLARE_CLASS(QTableView) class MainWindow : public QMainWindow { @@ -78,21 +29,20 @@ private slots: void addAlbum(); void changeArtist(int row); void deleteAlbum(); - void showAlbumDetails(QModelIndex index); - void showArtistProfile(QModelIndex index); - void updateHeader(QModelIndex, int, int); + void showAlbumDetails(const QModelIndex &index); + void showArtistProfile(const QModelIndex &index); + void adjustHeader(); private: - void adjustHeader(); QGroupBox *createAlbumGroupBox(); QGroupBox *createArtistGroupBox(); QGroupBox *createDetailsGroupBox(); void createMenuBar(); - void decreaseAlbumCount(QModelIndex artistIndex); + void decreaseAlbumCount(const QModelIndex &artistIndex); void getTrackList(QDomNode album); - QModelIndex indexOfArtist(const QString &artist); + QModelIndex indexOfArtist(const QString &artist) const; void readAlbumData(); - void removeAlbumFromDatabase(QModelIndex album); + void removeAlbumFromDatabase(const QModelIndex &album); void removeAlbumFromFile(int id); void showImageLabel(); |