diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-04-15 22:10:15 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-04-24 11:05:12 +0200 |
commit | 153b75db83ca4d3db26829bd85271e7fa88b402c (patch) | |
tree | 4257a712d61c086857a246bc12ee46e3125438b3 /examples/assistant | |
parent | 492fd5bb5ebbe4300a67e10aa0eb52a633c63bfe (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
Pick-to: 5.15
Change-Id: I923f44ce0eb27b7f73f9a0e16c11f48a80a0c03a
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
Diffstat (limited to 'examples/assistant')
6 files changed, 54 insertions, 45 deletions
diff --git a/examples/assistant/simpletextviewer/assistant.cpp b/examples/assistant/simpletextviewer/assistant.cpp index da29cea9e..d222bda6e 100644 --- a/examples/assistant/simpletextviewer/assistant.cpp +++ b/examples/assistant/simpletextviewer/assistant.cpp @@ -55,22 +55,18 @@ #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(), &QProcess::finished, nullptr, nullptr); + m_process->terminate(); + m_process->waitForFinished(3000); } - delete proc; } //! [0] @@ -83,7 +79,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 +103,17 @@ 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(), &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 +129,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 +145,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 9cae31045..e17f7ec0f 100644 --- a/examples/assistant/simpletextviewer/findfiledialog.cpp +++ b/examples/assistant/simpletextviewer/findfiledialog.cpp @@ -106,13 +106,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(); @@ -164,14 +162,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() @@ -189,10 +187,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() @@ -203,8 +201,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(); |