Jump to content

Qt for HarmonyOS/qtohosextras doc/Restart Application Example - main.cpp

From Qt Wiki
Revision as of 02:47, 17 September 2026 by Shawn Luo (talk | contribs) (Publish example source listings and restore Files lists (QTFOROH-2307))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

< Qt for HarmonyOS / qtohosextras doc

Restart Application Example - main.cpp

 // Copyright (C) 2025 The Qt Company Ltd.
 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

 #include <QApplication>
 #include <QJsonObject>
 #include <QJsonValue>
 #include <QLatin1String>
 #include <QPushButton>
 #include <QStandardItem>
 #include <QStandardItemModel>
 #include <QString>
 #include <QStringList>
 #include <QStringListIterator>
 #include <QTableView>
 #include <QVBoxLayout>
 #include <QWidget>
 #include <QtOhosExtras>
 #include <functional>
 #include <utility>

 namespace {

 QWidget *createActionButton(const QString &label, QWidget *parent, std::function<void()> action)
 {
     auto *button = new QPushButton(label, parent);
     QObject::connect(button, &QPushButton::clicked, parent, std::move(action));
     return button;
 }

 QStandardItemModel *makeQtParametersModel(
     QWidget *parent, const QStringList &headerLabels,
     const std::function<bool(const QString &)> &wantParametersFilterPredicate)
 {
     auto *model = new QStandardItemModel(parent);
     model->setHorizontalHeaderLabels(headerLabels);

     auto launchWant = QtOhosExtras::QOhosAppContext::getAppLaunchWant();

     QStringListIterator parameterKeysIterator(launchWant.parameters.keys());
     while (parameterKeysIterator.hasNext()) {
         auto key = parameterKeysIterator.next();
         if (wantParametersFilterPredicate(key)) {
             model->appendRow(
                 {
                     new QStandardItem(key),
                     new QStandardItem(launchWant.parameters[key].toVariant().toString())
                 });
         }
     }

     return model;
 }

 QWidget *createWantParametersTableView(
     QWidget *parent, const QStringList &headerLabels,
     const std::function<bool(const QString &)> &wantParametersFilterPredicate)
 {
     auto *tableView = new QTableView(parent);
     tableView->setModel(makeQtParametersModel(tableView, headerLabels, wantParametersFilterPredicate));

     tableView->resizeColumnsToContents();
     tableView->resizeRowsToContents();

     return tableView;
 }

 }

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);

     QWidget window;
     window.setWindowTitle(QLatin1String("Restart application example"));

     QVBoxLayout *layout = new QVBoxLayout(&window);
     layout->addWidget(
         createActionButton(
             QLatin1String("Restart application with command line parameters\n(all command line parameters will be preserved)"),
             &window,
             []() {
                 QtOhosExtras::QOhosAppContext::instance()->restartApp();
             }));
     layout->addWidget(
         createActionButton(
             QLatin1String("Restart application without command line parameters\n(all command line parameters will be lost)"),
             &window,
             []() {
                 auto want = QtOhosExtras::QOhosAppContext::getAppLaunchWant();
                 want.parameters = {};
                 QtOhosExtras::QOhosAppContext::instance()->restartApp(want);
             }));
     layout->addWidget(
         createWantParametersTableView(
             &window,
             {QLatin1String("Qt Parameter key"), QLatin1String("Qt Parameter value")},
             [](const QString &key) {
                 return key.startsWith(QLatin1String("io.qt"));
             }));
     window.setLayout(layout);
     window.show();

     return app.exec();
 }