Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to save files from and to usb ?



  • 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



  • 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);
    }
    

  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

    @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).



  • 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.


  • Lifetime Qt Champion

    @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.



  • 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.


  • Lifetime Qt Champion

    @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...



  • 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?


  • Lifetime Qt Champion



  • 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);
    }
    


  • @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.

Log in to reply