Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to save files from and to usb ?
Forum Updated to NodeBB v4.3 + New Features

How to save files from and to usb ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
12 Posts 3 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DiegOneD Offline
    DiegOneD Offline
    DiegOne
    wrote on last edited by
    #1

    Good afternoon,
    I have implemented a gallery that shows pictures that are present in a folder.
    My goal is to save files (images) from usb to the local gallery folder and the opposite.
    In particular for saving I need a File dialog that opens , I select the right usb location and the correct files and then the files are saved in the gallery destination path.
    Could you help me ? I am new in QT
    Thanks

    jsulmJ 1 Reply Last reply
    0
    • DiegOneD Offline
      DiegOneD Offline
      DiegOne
      wrote on last edited by
      #11

      I have found in another comment a snippet of code that helped me. This is my modified solution to open a file and copy a file from a location to another.

      QString FileDialog::openFolder(){
      
          QString usbFolderPath= QFileDialog::getExistingDirectory(Q_NULLPTR, "Open Directory", "",
                                                                   QFileDialog::ShowDirsOnly);
          return usbFolderPath;
      }
      
      
      bool FileDialog ::copyFiles() {
        const QString srcPath = QFileDialog::getOpenFileName(Q_NULLPTR, "Source file", "",
          "All files (*.*)");
        if (srcPath.isNull()) return false; // QFileDialog dialogs return null if user canceled
      
        const QString dstPath = QFileDialog::getSaveFileName(Q_NULLPTR, "Destination file", "file",
          "Images (*.png  *.jpg);; Videos (*.mov *.mp4"); // it asks the user for overwriting existing files
        if (dstPath.isNull()) return false;
      
        if (QFile::exists(dstPath))
          if (!QFile::remove(dstPath)) return false; // couldn't delete file
            // probably write-protected or insufficient privileges
      
        return QFile::copy(srcPath, dstPath);
      }
      
      1 Reply Last reply
      0
      • DiegOneD DiegOne

        Good afternoon,
        I have implemented a gallery that shows pictures that are present in a folder.
        My goal is to save files (images) from usb to the local gallery folder and the opposite.
        In particular for saving I need a File dialog that opens , I select the right usb location and the correct files and then the files are saved in the gallery destination path.
        Could you help me ? I am new in QT
        Thanks

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @DiegOne Do you want to do this in QML?
        I think at least copying files needs to be done in C++.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • DiegOneD Offline
          DiegOneD Offline
          DiegOne
          wrote on last edited by
          #3

          It's ok also using C++.
          I don't know how check my available USB, open a specific directory if a usb is available with a file dialog and then using the :

          QFile::copy("/path/file", "/path/copy-of-file");
          

          in order to copy a file to the folder previously selected in the file dialog.
          Thanks

          jsulmJ 1 Reply Last reply
          0
          • DiegOneD DiegOne

            It's ok also using C++.
            I don't know how check my available USB, open a specific directory if a usb is available with a file dialog and then using the :

            QFile::copy("/path/file", "/path/copy-of-file");
            

            in order to copy a file to the folder previously selected in the file dialog.
            Thanks

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @DiegOne On which platform are you?
            You can simply ask the user to select the files on the USB drive and then copy them to your gallery (you get the file paths from the file dialog, so no need to bother whether those are on USB drive or not). USB drives are usually mounted as a drive letter (Windows) or somewhere in the file system (like /media/username/USB_DRIVE_NAME on Linux).

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • DiegOneD Offline
              DiegOneD Offline
              DiegOne
              wrote on last edited by
              #5

              I am using a touch panel with a Raspberry 3b+ mounted with Linux. For this reason I need the integration with my qt application that is running on startup of the touchpanel.

              jsulmJ 1 Reply Last reply
              0
              • DiegOneD DiegOne

                I am using a touch panel with a Raspberry 3b+ mounted with Linux. For this reason I need the integration with my qt application that is running on startup of the touchpanel.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @DiegOne But why do you care whether files are on a USB drive or not? On Linux everything is mounted in one file system tree.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • DiegOneD Offline
                  DiegOneD Offline
                  DiegOne
                  wrote on last edited by DiegOne
                  #7

                  Imagine a gallery of images. Initially on the first startup of the application the gallery is empty. Later an user could insert a USB , copy images from the usb to the local gallery ( in the raspi) , remove the usb. Since the images are now saved also locally I can view them even if the usb is no more present.

                  Addittionaly, with my small application I could take picture with a camera. This pictures are saved in a local gallery folder. An user could insert a usb storage and copy the photos taken in the usb drive.

                  jsulmJ 1 Reply Last reply
                  0
                  • DiegOneD DiegOne

                    Imagine a gallery of images. Initially on the first startup of the application the gallery is empty. Later an user could insert a USB , copy images from the usb to the local gallery ( in the raspi) , remove the usb. Since the images are now saved also locally I can view them even if the usb is no more present.

                    Addittionaly, with my small application I could take picture with a camera. This pictures are saved in a local gallery folder. An user could insert a usb storage and copy the photos taken in the usb drive.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @DiegOne I understand your use case.
                    What I mean is that there is no special treatment needed for USB drives.
                    User connects a USB drives, goes to your app, opens the file dialog, navigates to the USB drive selects the images and your app copies them into gallery. Same for copying to USB drive - ask the user where he/she would like to store the files.
                    It doesn't matter whether it is USB drive, some local folder, a network drive...

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • DiegOneD Offline
                      DiegOneD Offline
                      DiegOne
                      wrote on last edited by
                      #9

                      Thank you very much @jsulm for your answers. My limit however is how to implement that you mention in the previous comment.
                      How to open a file dialog with c++ and copy the selected files to gallery?

                      jsulmJ 1 Reply Last reply
                      0
                      • DiegOneD DiegOne

                        Thank you very much @jsulm for your answers. My limit however is how to implement that you mention in the previous comment.
                        How to open a file dialog with c++ and copy the selected files to gallery?

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @DiegOne See https://doc.qt.io/qt-5/qfiledialog.html
                        https://doc.qt.io/qt-5/qfiledialog.html#getOpenFileNames
                        https://doc.qt.io/qt-5/qfile.html#copy

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2
                        • DiegOneD Offline
                          DiegOneD Offline
                          DiegOne
                          wrote on last edited by
                          #11

                          I have found in another comment a snippet of code that helped me. This is my modified solution to open a file and copy a file from a location to another.

                          QString FileDialog::openFolder(){
                          
                              QString usbFolderPath= QFileDialog::getExistingDirectory(Q_NULLPTR, "Open Directory", "",
                                                                                       QFileDialog::ShowDirsOnly);
                              return usbFolderPath;
                          }
                          
                          
                          bool FileDialog ::copyFiles() {
                            const QString srcPath = QFileDialog::getOpenFileName(Q_NULLPTR, "Source file", "",
                              "All files (*.*)");
                            if (srcPath.isNull()) return false; // QFileDialog dialogs return null if user canceled
                          
                            const QString dstPath = QFileDialog::getSaveFileName(Q_NULLPTR, "Destination file", "file",
                              "Images (*.png  *.jpg);; Videos (*.mov *.mp4"); // it asks the user for overwriting existing files
                            if (dstPath.isNull()) return false;
                          
                            if (QFile::exists(dstPath))
                              if (!QFile::remove(dstPath)) return false; // couldn't delete file
                                // probably write-protected or insufficient privileges
                          
                            return QFile::copy(srcPath, dstPath);
                          }
                          
                          1 Reply Last reply
                          0
                          • JoeCFDJ Offline
                            JoeCFDJ Offline
                            JoeCFD
                            wrote on last edited by JoeCFD
                            #12

                            @DiegOne said in How to save files from and to usb ?:

                            QFileDialog::getExistingDirectory

                            the usb path is here /media/username/

                            There may be more to be done:

                            1. progress bar(hard to impossible to do on linux if file size is too big)
                            2. stop in the middle of copying
                            3. QFile::copy(srcPath, dstPath) is not optimized
                            4. remind the user: do not remove usb in the middle of copy since usb can be damaged. it is better to add a button to let users remove usb safely.
                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved