Don't use file dialog in the address book example

On small screen devices such as iPhone targets, the save file dialog is
using a non-native dialog, and it doesn't fit the screen real estate to
the extent that the [Ok] button is clipped away.

In addition, the open file dialog and the save file dialog doesn't
cooperate very well on platforms such as iOS without more plumbing.
Since using the file dialog is out of the scope for this example we
remove all usages of it.

Change-Id: Ie165355ed0b671d93e44d2d55791156367b0ea5c
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
(cherry picked from commit 4dbb07f614c914e78437aacf7a1716f632da5e8a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Jan Arve Sæther 2023-01-12 14:33:09 +01:00 committed by Qt Cherry-pick Bot
parent 61f7b89e6f
commit be95bb0358
4 changed files with 17 additions and 19 deletions

View File

@ -381,17 +381,15 @@
respective slots, we also connect \c AddressWidget's respective slots, we also connect \c AddressWidget's
\c selectionChanged() signal to its \c updateActions() slot. \c selectionChanged() signal to its \c updateActions() slot.
The \c openFile() function allows the user to choose a file with The \c openFile() function opens a custom \c{addressbook.dat} file that
the \l{QFileDialog::getOpenFileName()}{open file dialog}. The chosen contains address book contacts. This function is a slot connected to
file has to be a custom \c{.dat} file that contains address book \c openAct in the \uicontrol File menu.
contacts. This function is a slot connected to \c openAct in the
\uicontrol File menu.
\snippet itemviews/addressbook/mainwindow.cpp 2 \snippet itemviews/addressbook/mainwindow.cpp 2
The \c saveFile() function allows the user to save a file with The \c saveFile() function saves a custom \c{addressbook.dat} file that
the \l{QFileDialog::getSaveFileName()}{save file dialog}. This function will contain the address book contacts. This function is a slot connected
is a slot connected to \c saveAct in the \uicontrol File menu. to \c saveAct in the \uicontrol File menu.
\snippet itemviews/addressbook/mainwindow.cpp 3 \snippet itemviews/addressbook/mainwindow.cpp 3

View File

@ -146,7 +146,7 @@ void AddressWidget::setupTabs()
//! [1] //! [1]
//! [7] //! [7]
void AddressWidget::readFromFile(const QString &fileName) void AddressWidget::readFromFile()
{ {
QFile file(fileName); QFile file(fileName);
@ -171,7 +171,7 @@ void AddressWidget::readFromFile(const QString &fileName)
//! [7] //! [7]
//! [6] //! [6]
void AddressWidget::writeToFile(const QString &fileName) void AddressWidget::writeToFile()
{ {
QFile file(fileName); QFile file(fileName);

View File

@ -9,6 +9,7 @@
#include <QItemSelection> #include <QItemSelection>
#include <QTabWidget> #include <QTabWidget>
#include <QStandardPaths>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QSortFilterProxyModel; class QSortFilterProxyModel;
@ -22,8 +23,8 @@ class AddressWidget : public QTabWidget
public: public:
AddressWidget(QWidget *parent = nullptr); AddressWidget(QWidget *parent = nullptr);
void readFromFile(const QString &fileName); void readFromFile();
void writeToFile(const QString &fileName); void writeToFile();
public slots: public slots:
void showAddEntryDialog(); void showAddEntryDialog();
@ -37,6 +38,9 @@ signals:
private: private:
void setupTabs(); void setupTabs();
inline static QString fileName =
QStandardPaths::standardLocations(QStandardPaths::TempLocation).value(0)
+ QStringLiteral("/addressbook.dat");
TableModel *table; TableModel *table;
NewAddressTab *newAddressTab; NewAddressTab *newAddressTab;
}; };

View File

@ -28,7 +28,7 @@ void MainWindow::createMenus()
connect(openAct, &QAction::triggered, this, &MainWindow::openFile); connect(openAct, &QAction::triggered, this, &MainWindow::openFile);
//! [1a] //! [1a]
QAction *saveAct = new QAction(tr("&Save As..."), this); QAction *saveAct = new QAction(tr("&Save"), this);
fileMenu->addAction(saveAct); fileMenu->addAction(saveAct);
connect(saveAct, &QAction::triggered, this, &MainWindow::saveFile); connect(saveAct, &QAction::triggered, this, &MainWindow::saveFile);
@ -66,18 +66,14 @@ void MainWindow::createMenus()
//! [2] //! [2]
void MainWindow::openFile() void MainWindow::openFile()
{ {
QString fileName = QFileDialog::getOpenFileName(this); addressWidget->readFromFile();
if (!fileName.isEmpty())
addressWidget->readFromFile(fileName);
} }
//! [2] //! [2]
//! [3] //! [3]
void MainWindow::saveFile() void MainWindow::saveFile()
{ {
QString fileName = QFileDialog::getSaveFileName(this); addressWidget->writeToFile();
if (!fileName.isEmpty())
addressWidget->writeToFile(fileName);
} }
//! [3] //! [3]