summaryrefslogtreecommitdiffstats
path: root/examples/pdfwidgets/pdfviewer
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-09-07 14:21:33 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-09-07 20:58:18 +0200
commit1124186319bc4f9a395ba631b769e9bff5e08fe9 (patch)
treedc48769ad3d515be8e0fb20114857ca1c399e7df /examples/pdfwidgets/pdfviewer
parenta14c5924d26fa73624269e0d46f914a8ec3471da (diff)
Polish the PdfWidgets/PdfViewer example
- Use QCommandLineParser - Fix up QFileDialog handling - Small fixes Pick-to: 6.4 6.3 Change-Id: I0cbe30a0fb5c856805956970407aa526d1d93fa4 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'examples/pdfwidgets/pdfviewer')
-rw-r--r--examples/pdfwidgets/pdfviewer/main.cpp17
-rw-r--r--examples/pdfwidgets/pdfviewer/mainwindow.cpp23
-rw-r--r--examples/pdfwidgets/pdfviewer/mainwindow.h2
3 files changed, 33 insertions, 9 deletions
diff --git a/examples/pdfwidgets/pdfviewer/main.cpp b/examples/pdfwidgets/pdfviewer/main.cpp
index 7df5fe253..6b890e0ac 100644
--- a/examples/pdfwidgets/pdfviewer/main.cpp
+++ b/examples/pdfwidgets/pdfviewer/main.cpp
@@ -3,16 +3,27 @@
#include "mainwindow.h"
#include <QApplication>
+#include <QCommandLineParser>
+#include <QCommandLineOption>
#include <QUrl>
int main(int argc, char *argv[])
{
+ QCoreApplication::setApplicationName("Qt PDF Viewer");
+ QCoreApplication::setOrganizationName("QtProject");
+
QApplication a(argc, argv);
+
+ QCommandLineParser parser;
+ parser.addHelpOption();
+ parser.addVersionOption();
+ parser.addPositionalArgument("file", "The file to open.");
+ parser.process(a);
+
MainWindow w;
- QStringList args = a.arguments();
w.show();
- if (args.length() > 1)
- w.open(QUrl::fromLocalFile(args[1]));
+ if (!parser.positionalArguments().isEmpty())
+ w.open(QUrl::fromLocalFile(parser.positionalArguments().constFirst()));
return a.exec();
}
diff --git a/examples/pdfwidgets/pdfviewer/mainwindow.cpp b/examples/pdfwidgets/pdfviewer/mainwindow.cpp
index 2358b6b4a..02caffa48 100644
--- a/examples/pdfwidgets/pdfviewer/mainwindow.cpp
+++ b/examples/pdfwidgets/pdfviewer/mainwindow.cpp
@@ -12,6 +12,7 @@
#include <QPdfBookmarkModel>
#include <QPdfDocument>
#include <QPdfPageNavigator>
+#include <QStandardPaths>
#include <QtMath>
const qreal zoomMultiplier = qSqrt(2.0);
@@ -45,7 +46,7 @@ MainWindow::MainWindow(QWidget *parent)
bookmarkModel->setDocument(m_document);
ui->bookmarkView->setModel(bookmarkModel);
- connect(ui->bookmarkView, SIGNAL(activated(QModelIndex)), this, SLOT(bookmarkSelected(QModelIndex)));
+ connect(ui->bookmarkView, &QAbstractItemView::activated, this, &MainWindow::bookmarkSelected);
ui->tabWidget->setTabEnabled(1, false); // disable 'Pages' tab for now
@@ -69,8 +70,9 @@ void MainWindow::open(const QUrl &docLocation)
pageSelected(0);
m_pageSelector->setMaximum(m_document->pageCount() - 1);
} else {
- qCDebug(lcExample) << docLocation << "is not a valid local file";
- QMessageBox::critical(this, tr("Failed to open"), tr("%1 is not a valid local file").arg(docLocation.toString()));
+ const QString message = tr("%1 is not a valid local file").arg(docLocation.toString());
+ qCDebug(lcExample).noquote() << message;
+ QMessageBox::critical(this, tr("Failed to open"), message);
}
qCDebug(lcExample) << docLocation;
}
@@ -93,9 +95,18 @@ void MainWindow::pageSelected(int page)
void MainWindow::on_actionOpen_triggered()
{
- QUrl toOpen = QFileDialog::getOpenFileUrl(this, tr("Choose a PDF"), QUrl(), "Portable Documents (*.pdf)");
- if (toOpen.isValid())
- open(toOpen);
+ if (m_fileDialog == nullptr) {
+ m_fileDialog = new QFileDialog(this, tr("Choose a PDF"),
+ QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
+ m_fileDialog->setAcceptMode(QFileDialog::AcceptOpen);
+ m_fileDialog->setMimeTypeFilters({"application/pdf"});
+ }
+
+ if (m_fileDialog->exec() == QDialog::Accepted) {
+ const QUrl toOpen = m_fileDialog->selectedUrls().constFirst();
+ if (toOpen.isValid())
+ open(toOpen);
+ }
}
void MainWindow::on_actionQuit_triggered()
diff --git a/examples/pdfwidgets/pdfviewer/mainwindow.h b/examples/pdfwidgets/pdfviewer/mainwindow.h
index 74349b5e5..958e11061 100644
--- a/examples/pdfwidgets/pdfviewer/mainwindow.h
+++ b/examples/pdfwidgets/pdfviewer/mainwindow.h
@@ -14,6 +14,7 @@ namespace Ui {
class MainWindow;
}
+class QFileDialog;
class QPdfDocument;
class QPdfView;
class QSpinBox;
@@ -53,6 +54,7 @@ private:
Ui::MainWindow *ui;
ZoomSelector *m_zoomSelector;
QSpinBox *m_pageSelector;
+ QFileDialog *m_fileDialog = nullptr;
QPdfDocument *m_document;
};