Jump to content

Qt for HarmonyOS/qtohosextras doc/File Utils Example - main.cpp

From Qt Wiki
Revision as of 02:38, 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

File Utils 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 <QFileDialog>
 #include <QFileInfo>
 #include <QLatin1String>
 #include <QMessageBox>
 #include <QPushButton>
 #include <QString>
 #include <QVBoxLayout>
 #include <QWidget>
 #include <QtOhosExtras>

 namespace {

 QPushButton *createMoveToTrashButton(QWidget *parent)
 {
     auto *moveToTrashButton = new QPushButton(QLatin1String("Select file to move to Trash"), parent);

     QObject::connect(
         moveToTrashButton, &QPushButton::clicked, parent,
         [parent]() {
             auto filePath = QFileDialog::getOpenFileName(
                 parent, QLatin1String("Select file to move to Trash"));
             if (filePath.isEmpty()) {
                 return;
             }

             bool movedToTrash = QtOhosExtras::moveFileToTrash(filePath);

             auto fileName = QFileInfo(filePath).fileName();
             if (movedToTrash) {
                 QMessageBox::information(
                     parent, QLatin1String("Moved to Trash"),
                     QString(QLatin1String("File '%1' moved to Trash'")).arg(fileName));
             } else {
                 QMessageBox::warning(
                     parent, QLatin1String("Failed to move to Trash"),
                     QString(QLatin1String("Failed to move file '%1' to Trash'")).arg(fileName));
             }
         });

     return moveToTrashButton;
 }

 }

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

     QWidget window;
     window.setWindowTitle(QLatin1String("OHOS File Utils"));

     QVBoxLayout *layout = new QVBoxLayout(&window);

     auto *moveToTrashButton = createMoveToTrashButton(&window);
     layout->addWidget(moveToTrashButton);

     window.setLayout(layout);
     window.show();

     return app.exec();
 }