Get path from usb connected



  • Hello to all.

    I would like the next:
    1-Detect USB is connected.
    2-See on it and if there is a folder colled "folder1" then show a message.

    That is very simple but I do not know how to get the path of usb connected.
    At the moment I have done the first step "detect usb is connected" whit the next code:

        FSwatcher = new QFileSystemWatcher(this);
        std::string user = getenv ("USER");
        std::string path = "/media/" + user;
        FSwatcher->addPath(path.c_str());
    
        connect(FSwatcher, SIGNAL(directoryChanged(QString)), SLOT(usbDetected()));
    
    void MainWindow::usbDetected()
    {
        QMessageBox msgBox;
        msgBox.setText("USB Detected.");
        msgBox.exec();
    }
    

    The problem is that image that the usb has two partition.
    So the path of each partition is (on Linux) : /media/user/xx1 and /media/user/xx2
    So hoy can I get the name "xx1/xx2"?

    Thanks a lot!



  • You can use QDir to read the contents of /media and read the name of the file interested.



  • Thanks for replying.
    I have used what you say and it works!

    Now I have a question.
    I use this:

    QDir directory(path.c_str());
    QStringList FilesList = directory.entryList();
    

    FilesList is a list which name of usb connected.
    Now I have 2 USB connected, and FilesList returns me this:
    <4 items> [0] ".."
    [1] ".."
    [2] "5CFD-0"
    [3] "5F82-1"
    So items [2] and [3] are correct but I do not know what items [0] and [1] are. Do they appear always by default?

    Thanks again!


  • Moderators

    @AlvaroS One point means current directory, two points means parent directory. This is standard and is shown by default.
    Use QDir::NoDotAndDotDot as parameter to entryList() to get rid of them.


  • Moderators

    @AlvaroS

    StringList FilesList = directory.entryList( QDir::AllDirs |  QDir::NoDotAndDotDot );
    


  • @jsulm said in Get path from usb connected:

    @AlvaroS One point means current directory, two points means parent directory. This is standard and is shown by default.
    Use QDir::NoDotAndDotDot as parameter to entryList() to get rid of them.

    @raven-worx said in Get path from usb connected:

    @AlvaroS

    StringList FilesList = directory.entryList( QDir::AllDirs |  QDir::NoDotAndDotDot );
    

    Yes! It works!
    Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.