Transfer Files from Qt Creator to External USB Port



  • Re: USB Communication Using Qt
    I am a newb in Qt. I am looking for someone to help me with a problem "to transfer files from Ubuntu to External USB Drive".
    #qt



  • The OS should do all the work for you, QFile::copy() should be enough to achieve what you want



  • It's a project that needs undefined USBs. Means path of the Pendrive will change with new Pendrive. So is there any way to get the path of that device dynamically and use it to copy files to that device.

    Can't find the way to do that.



  • @Jimit-Rupani
    you can use QStorageInfo::mountedVolumes . Connected USB-Drives should be part of that list.

    foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
              if (storage.isValid() && storage.isReady()) {
                  if (!storage.isReadOnly()) {
                      // ...
                  }
              }
          }
    


  • I have solved the problem with the QStorageInfo::mountedVolumes() which return the list of the devices that are connected to the Machine.But all of them won't have a name except the Pendrive or HDD. So (!(storage.name()).isEmpty())) it will return the path to only those devices.

    QString location;
    QString path1= "/Report/1.txt";
    QString locationoffolder="/Report";
    
    foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
              if (storage.isValid() && storage.isReady() && (!(storage.name()).isEmpty())) {
                  if (!storage.isReadOnly()) {
    
                     qDebug() << "path:" << storage.rootPath();
                     //WILL CREATE A FILE IN A BUILD FOLDER
                     location = storage.rootPath();
                     QString srcPath = "writable.txt";
                     //PATH OF THE FOLDER IN PENDRIVE
                     QString destPath = location+path1;
                     QString folderdir = location+locationoffolder;
                     //IF FOLDER IS NOT IN PENDRIVE THEN IT WILL CREATE A FOLDER NAME REPORT
                     QDir dir(folderdir);
                     if(!dir.exists()){
                        dir.mkpath(".");
                     }
    
                     qDebug() << "Usbpath:" <<destPath;
                     if (QFile::exists(destPath)) QFile::remove(destPath);
                     QFile::copy(srcPath,destPath);
                     qDebug("copied");
    
                  }
              }
        }
    }
    

    I had to create a folder as well as in USB because of my requirements and I have given a static name for the files. then I just copied the data from file of the local machine to the file which I have created in USB with the help of QFile::copy(srcPath, dstPath). I hope this will help someone.



  • @J.Hilk I did and it solved my problem. thank you for the help!!


  • Moderators



  • @jsulm I have already try that all I got is just P.Id and V.id of pen drive. but I am not able to transfer the data from the machine. our project will deploy in a machine and from there it has to transfer data from machine to USB connected to it's port. from above solution, i am able to transfer data from PC to ps's USB port. But it's not working with the device. and thank you for the suggestions.


  • Lifetime Qt Champion

    Hi,

    What are you running on that device of yours ?



  • @SGaist It's a device from toradex which is using light weight linux. it'a titration device.


  • Lifetime Qt Champion

    What distribution is your "light weight linux" ?



  • @SGaist yup it's light weight and we don't have console also in that devices Linux.


  • Lifetime Qt Champion

    What are you using for device handling with your distribution ?



  • @SGaist sorry i didn't get you?


  • Lifetime Qt Champion

    What service/library/etc. are you using on your target to handle devices that you connect to it through USB (e.g. udev) ?


Log in to reply