diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-04-15 22:10:15 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-05-19 08:43:35 +0200 |
commit | bed49e664c0ae6719e712c4750feb2b704484977 (patch) | |
tree | d0b654aa95522aa798c93183e44cbd44fa5a6bda | |
parent | 27f8fe5b21ac555cda9d16b2581f02a4305df1ea (diff) |
Polish the SimpleTextViewer example
- Port to Qt 5 connection syntax, fixing error in Qt 6
QObject::connect: No such signal QComboBox::currentIndexChanged(QString) in findfiledialog.cpp:194
- Remove unused functions and parameters to avoid QOverload
- Add process error handling
Change-Id: I923f44ce0eb27b7f73f9a0e16c11f48a80a0c03a
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
(cherry picked from commit 153b75db83ca4d3db26829bd85271e7fa88b402c)
6 files changed, 57 insertions, 45 deletions
diff --git a/examples/assistant/simpletextviewer/assistant.cpp b/examples/assistant/simpletextviewer/assistant.cpp index da29cea9e..ceda2c384 100644 --- a/examples/assistant/simpletextviewer/assistant.cpp +++ b/examples/assistant/simpletextviewer/assistant.cpp @@ -55,22 +55,20 @@ #include <QDir> #include <QLibraryInfo> #include <QMessageBox> -#include <QProcess> #include <QStandardPaths> -Assistant::Assistant() - : proc(0) -{ -} +Assistant::Assistant() = default; //! [0] Assistant::~Assistant() { - if (proc && proc->state() == QProcess::Running) { - proc->terminate(); - proc->waitForFinished(3000); + if (!m_process.isNull() && m_process->state() == QProcess::Running) { + QObject::disconnect(m_process.data(), + QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), + nullptr, nullptr); + m_process->terminate(); + m_process->waitForFinished(3000); } - delete proc; } //! [0] @@ -83,7 +81,7 @@ void Assistant::showDocumentation(const QString &page) QByteArray ba("SetSource "); ba.append("qthelp://org.qt-project.examples.simpletextviewer/doc/"); - proc->write(ba + page.toLocal8Bit() + '\n'); + m_process->write(ba + page.toLocal8Bit() + '\n'); } //! [1] @@ -107,12 +105,18 @@ QString documentationDirectory() //! [2] bool Assistant::startAssistant() { - if (!proc) - proc = new QProcess(); + if (m_process.isNull()) { + m_process.reset(new QProcess()); + QObject::connect(m_process.data(), + QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), + m_process.data(), [this](int exitCode, QProcess::ExitStatus status) { + this->finished(exitCode, status); + }); + } - if (proc->state() != QProcess::Running) { + if (m_process->state() != QProcess::Running) { QString app = QLibraryInfo::location(QLibraryInfo::BinariesPath) + QDir::separator(); -#if !defined(Q_OS_MAC) +#ifndef Q_OS_DARWIN app += QLatin1String("assistant"); #else app += QLatin1String("Assistant.app/Contents/MacOS/Assistant"); @@ -128,10 +132,10 @@ bool Assistant::startAssistant() collectionDirectory + QLatin1String("/simpletextviewer.qhc"), QLatin1String("-enableRemoteControl")}; - proc->start(app, args); + m_process->start(app, args); - if (!proc->waitForStarted()) { - showError(tr("Unable to launch Qt Assistant (%1): %2").arg(app, proc->errorString())); + if (!m_process->waitForStarted()) { + showError(tr("Unable to launch Qt Assistant (%1): %2").arg(app, m_process->errorString())); return false; } } @@ -144,3 +148,13 @@ void Assistant::showError(const QString &message) QMessageBox::critical(QApplication::activeWindow(), tr("Simple Text Viewer"), message); } + +void Assistant::finished(int exitCode, QProcess::ExitStatus status) +{ + const QString stdErr = QString::fromLocal8Bit(m_process->readAllStandardError()); + if (status != QProcess::NormalExit) { + showError(tr("Assistant crashed: ").arg(stdErr)); + } else if (exitCode != 0) { + showError(tr("Assistant exited with %1: %2").arg(exitCode).arg(stdErr)); + } +} diff --git a/examples/assistant/simpletextviewer/assistant.h b/examples/assistant/simpletextviewer/assistant.h index c9b8f382c..c513c3428 100644 --- a/examples/assistant/simpletextviewer/assistant.h +++ b/examples/assistant/simpletextviewer/assistant.h @@ -52,11 +52,10 @@ #define ASSISTANT_H #include <QCoreApplication> +#include <QProcess> +#include <QScopedPointer> #include <QString> -QT_BEGIN_NAMESPACE -class QProcess; -QT_END_NAMESPACE class Assistant { @@ -70,7 +69,9 @@ public: private: bool startAssistant(); void showError(const QString &message); - QProcess *proc; + void finished(int exitCode, QProcess::ExitStatus status); + + QScopedPointer<QProcess> m_process; }; #endif diff --git a/examples/assistant/simpletextviewer/findfiledialog.cpp b/examples/assistant/simpletextviewer/findfiledialog.cpp index bb47ae4fd..1eab5ad7f 100644 --- a/examples/assistant/simpletextviewer/findfiledialog.cpp +++ b/examples/assistant/simpletextviewer/findfiledialog.cpp @@ -105,13 +105,11 @@ void FindFileDialog::help() } //! [2] -void FindFileDialog::openFile(QTreeWidgetItem *item) +void FindFileDialog::openFile() { - if (!item) { - item = foundFilesTree->currentItem(); - if (!item) - return; - } + auto item = foundFilesTree->currentItem(); + if (!item) + return; QString fileName = item->text(0); QString path = directoryComboBox->currentText() + QDir::separator(); @@ -161,14 +159,14 @@ void FindFileDialog::createButtons() { browseButton = new QToolButton; browseButton->setText(tr("...")); - connect(browseButton, SIGNAL(clicked()), this, SLOT(browse())); + connect(browseButton, &QAbstractButton::clicked, this, &FindFileDialog::browse); buttonBox = new QDialogButtonBox(QDialogButtonBox::Open | QDialogButtonBox::Cancel | QDialogButtonBox::Help); - connect(buttonBox, SIGNAL(accepted()), this, SLOT(openFile())); - connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); - connect(buttonBox, SIGNAL(helpRequested()), this, SLOT(help())); + connect(buttonBox, &QDialogButtonBox::accepted, this, &FindFileDialog::openFile); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); + connect(buttonBox, &QDialogButtonBox::helpRequested, this, &FindFileDialog::help); } void FindFileDialog::createComboBoxes() @@ -186,10 +184,10 @@ void FindFileDialog::createComboBoxes() directoryComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); - connect(fileNameComboBox, SIGNAL(editTextChanged(QString)), - this, SLOT(update())); - connect(directoryComboBox, SIGNAL(currentIndexChanged(QString)), - this, SLOT(update())); + connect(fileNameComboBox, &QComboBox::editTextChanged, + this, &FindFileDialog::update); + connect(directoryComboBox, &QComboBox::currentTextChanged, + this, &FindFileDialog::update); } void FindFileDialog::createFilesTree() @@ -200,8 +198,8 @@ void FindFileDialog::createFilesTree() foundFilesTree->setRootIsDecorated(false); foundFilesTree->setSelectionMode(QAbstractItemView::SingleSelection); - connect(foundFilesTree, SIGNAL(itemActivated(QTreeWidgetItem*,int)), - this, SLOT(openFile(QTreeWidgetItem*))); + connect(foundFilesTree, &QTreeWidget::itemActivated, + this, &FindFileDialog::openFile); } void FindFileDialog::createLabels() diff --git a/examples/assistant/simpletextviewer/findfiledialog.h b/examples/assistant/simpletextviewer/findfiledialog.h index 6ca57b061..605da0954 100644 --- a/examples/assistant/simpletextviewer/findfiledialog.h +++ b/examples/assistant/simpletextviewer/findfiledialog.h @@ -76,7 +76,7 @@ public: private slots: void browse(); void help(); - void openFile(QTreeWidgetItem *item = nullptr); + void openFile(); void update(); private: diff --git a/examples/assistant/simpletextviewer/mainwindow.cpp b/examples/assistant/simpletextviewer/mainwindow.cpp index 5b0564ca1..f70162c21 100644 --- a/examples/assistant/simpletextviewer/mainwindow.cpp +++ b/examples/assistant/simpletextviewer/mainwindow.cpp @@ -112,26 +112,26 @@ void MainWindow::createActions() { assistantAct = new QAction(tr("Help Contents"), this); assistantAct->setShortcut(QKeySequence::HelpContents); - connect(assistantAct, SIGNAL(triggered()), this, SLOT(showDocumentation())); + connect(assistantAct, &QAction::triggered, this, &MainWindow::showDocumentation); //! [4] openAct = new QAction(tr("&Open..."), this); openAct->setShortcut(QKeySequence::Open); - connect(openAct, SIGNAL(triggered()), this, SLOT(open())); + connect(openAct, &QAction::triggered, this, &MainWindow::open); clearAct = new QAction(tr("&Clear"), this); clearAct->setShortcut(tr("Ctrl+C")); - connect(clearAct, SIGNAL(triggered()), textViewer, SLOT(clear())); + connect(clearAct, &QAction::triggered, textViewer, &QTextEdit::clear); exitAct = new QAction(tr("E&xit"), this); exitAct->setShortcuts(QKeySequence::Quit); - connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); + connect(exitAct, &QAction::triggered, this, &QWidget::close); aboutAct = new QAction(tr("&About"), this); - connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); + connect(aboutAct, &QAction::triggered, this, &MainWindow::about); aboutQtAct = new QAction(tr("About &Qt"), this); - connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); + connect(aboutQtAct, &QAction::triggered, QApplication::aboutQt); //! [5] } //! [5] diff --git a/examples/assistant/simpletextviewer/mainwindow.h b/examples/assistant/simpletextviewer/mainwindow.h index e8e917ea3..1c7f21761 100644 --- a/examples/assistant/simpletextviewer/mainwindow.h +++ b/examples/assistant/simpletextviewer/mainwindow.h @@ -67,7 +67,6 @@ class MainWindow : public QMainWindow public: MainWindow(); - void showDocumentation(const QString &file); private slots: void about(); |