summaryrefslogtreecommitdiffstats
path: root/examples/widgets/itemviews/addressbook
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/itemviews/addressbook')
-rw-r--r--examples/widgets/itemviews/addressbook/CMakeLists.txt34
-rw-r--r--examples/widgets/itemviews/addressbook/addresswidget.cpp19
-rw-r--r--examples/widgets/itemviews/addressbook/addresswidget.h8
-rw-r--r--examples/widgets/itemviews/addressbook/mainwindow.cpp10
4 files changed, 40 insertions, 31 deletions
diff --git a/examples/widgets/itemviews/addressbook/CMakeLists.txt b/examples/widgets/itemviews/addressbook/CMakeLists.txt
index 1c440451e7..91e9cd03c9 100644
--- a/examples/widgets/itemviews/addressbook/CMakeLists.txt
+++ b/examples/widgets/itemviews/addressbook/CMakeLists.txt
@@ -1,16 +1,13 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
cmake_minimum_required(VERSION 3.16)
project(addressbook LANGUAGES CXX)
-set(CMAKE_AUTOMOC ON)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/itemviews/addressbook")
-
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
+qt_standard_project_setup()
+
qt_add_executable(addressbook
adddialog.cpp adddialog.h
addresswidget.cpp addresswidget.h
@@ -25,14 +22,21 @@ set_target_properties(addressbook PROPERTIES
MACOSX_BUNDLE TRUE
)
-target_link_libraries(addressbook PUBLIC
- Qt::Core
- Qt::Gui
- Qt::Widgets
+target_link_libraries(addressbook PRIVATE
+ Qt6::Core
+ Qt6::Gui
+ Qt6::Widgets
)
install(TARGETS addressbook
- 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 addressbook
+ OUTPUT_SCRIPT deploy_script
+ NO_UNSUPPORTED_PLATFORM_ERROR
)
+install(SCRIPT ${deploy_script})
diff --git a/examples/widgets/itemviews/addressbook/addresswidget.cpp b/examples/widgets/itemviews/addressbook/addresswidget.cpp
index de6b57e9d7..5b226793e4 100644
--- a/examples/widgets/itemviews/addressbook/addresswidget.cpp
+++ b/examples/widgets/itemviews/addressbook/addresswidget.cpp
@@ -34,7 +34,10 @@ void AddressWidget::showAddEntryDialog()
//! [3]
void AddressWidget::addEntry(const QString &name, const QString &address)
{
- if (!table->getContacts().contains({ name, address })) {
+ if (!name.front().isLetter()) {
+ QMessageBox::information(this, tr("Invalid name"),
+ tr("The name must start with a letter."));
+ } else if (!table->getContacts().contains({ name, address })) {
table->insertRows(0, 1, QModelIndex());
QModelIndex index = table->index(0, 0, QModelIndex());
@@ -110,10 +113,12 @@ void AddressWidget::removeEntry()
//! [1]
void AddressWidget::setupTabs()
{
- const auto groups = { "ABC", "DEF", "GHI", "JKL", "MNO", "PQR", "STU", "VW", "XYZ" };
+ using namespace Qt::StringLiterals;
+ const auto groups = { "ABC"_L1, "DEF"_L1, "GHI"_L1, "JKL"_L1, "MNO"_L1, "PQR"_L1,
+ "STU"_L1, "VW"_L1, "XYZ"_L1 };
- for (const QString &str : groups) {
- const auto regExp = QRegularExpression(QString("^[%1].*").arg(str),
+ for (QLatin1StringView str : groups) {
+ const auto regExp = QRegularExpression(QLatin1StringView("^[%1].*").arg(str),
QRegularExpression::CaseInsensitiveOption);
auto proxyModel = new QSortFilterProxyModel(this);
@@ -144,7 +149,7 @@ void AddressWidget::setupTabs()
//! [1]
//! [7]
-void AddressWidget::readFromFile(const QString &fileName)
+void AddressWidget::readFromFile()
{
QFile file(fileName);
@@ -162,14 +167,14 @@ void AddressWidget::readFromFile(const QString &fileName)
QMessageBox::information(this, tr("No contacts in file"),
tr("The file you are attempting to open contains no contacts."));
} else {
- for (const auto &contact: qAsConst(contacts))
+ for (const auto &contact: std::as_const(contacts))
addEntry(contact.name, contact.address);
}
}
//! [7]
//! [6]
-void AddressWidget::writeToFile(const QString &fileName)
+void AddressWidget::writeToFile()
{
QFile file(fileName);
diff --git a/examples/widgets/itemviews/addressbook/addresswidget.h b/examples/widgets/itemviews/addressbook/addresswidget.h
index e9a910ef8f..c5c710286a 100644
--- a/examples/widgets/itemviews/addressbook/addresswidget.h
+++ b/examples/widgets/itemviews/addressbook/addresswidget.h
@@ -9,6 +9,7 @@
#include <QItemSelection>
#include <QTabWidget>
+#include <QStandardPaths>
QT_BEGIN_NAMESPACE
class QSortFilterProxyModel;
@@ -22,8 +23,8 @@ class AddressWidget : public QTabWidget
public:
AddressWidget(QWidget *parent = nullptr);
- void readFromFile(const QString &fileName);
- void writeToFile(const QString &fileName);
+ void readFromFile();
+ void writeToFile();
public slots:
void showAddEntryDialog();
@@ -37,6 +38,9 @@ signals:
private:
void setupTabs();
+ inline static QString fileName =
+ QStandardPaths::standardLocations(QStandardPaths::TempLocation).value(0)
+ + QStringLiteral("/addressbook.dat");
TableModel *table;
NewAddressTab *newAddressTab;
};
diff --git a/examples/widgets/itemviews/addressbook/mainwindow.cpp b/examples/widgets/itemviews/addressbook/mainwindow.cpp
index 6047047e9f..ce37b523f3 100644
--- a/examples/widgets/itemviews/addressbook/mainwindow.cpp
+++ b/examples/widgets/itemviews/addressbook/mainwindow.cpp
@@ -28,7 +28,7 @@ void MainWindow::createMenus()
connect(openAct, &QAction::triggered, this, &MainWindow::openFile);
//! [1a]
- QAction *saveAct = new QAction(tr("&Save As..."), this);
+ QAction *saveAct = new QAction(tr("&Save"), this);
fileMenu->addAction(saveAct);
connect(saveAct, &QAction::triggered, this, &MainWindow::saveFile);
@@ -66,18 +66,14 @@ void MainWindow::createMenus()
//! [2]
void MainWindow::openFile()
{
- QString fileName = QFileDialog::getOpenFileName(this);
- if (!fileName.isEmpty())
- addressWidget->readFromFile(fileName);
+ addressWidget->readFromFile();
}
//! [2]
//! [3]
void MainWindow::saveFile()
{
- QString fileName = QFileDialog::getSaveFileName(this);
- if (!fileName.isEmpty())
- addressWidget->writeToFile(fileName);
+ addressWidget->writeToFile();
}
//! [3]