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

qfile link function not working



  • my system is linux,qt5.11
    i am trying to make shortcut of file using qfile::link .
    when i give both file path diractly it creates a link.but when i tried to give the both path using qfileinfo it creates a broken file.
    here is the code.

        QString newPath;
        if (!curIndex.isDir()) newPath = curIndex.filePath();
        else newPath = ui->pathEdit->text();
    
        QFileInfo f(QApplication::clipboard()->text());
        QString a(f.fileName());
        QString sf(newPath+ "/" + a);
    
        qDebug() << QApplication::clipboard()->text() << sf ;
        QString o(QApplication::clipboard()->text());
    
        QFile::link(o,sf); //not working
    
        QFile::link("/home/shaber/Downloads/gh/NH.png","/home/shaber/Downloads/NH.png"); // works
    

  • Lifetime Qt Champion

    @saber

    Please simplify the code so you get QClipboard out. You may have big chances that the problem is within the clipboard code.



  • @saber here is simplest i can get.

        QString newPath;
        if (!curIndex.isDir()) newPath = curIndex.filePath();
        else newPath = ui->pathEdit->text();
    
        QFileInfo f(QApplication::clipboard()->text());
        QString a(f.fileName());
        QString sf(newPath+ "/" + a);
    
        qDebug() << QApplication::clipboard()->text() << sf ;
    
        QFile::link(QApplication::clipboard()->text(),sf); //not working
    
        QFile::link("/home/shaber/Downloads/gh/NH.png","/home/shaber/Downloads/NH.png"); // works
    

  • Lifetime Qt Champion

    Hi,

    What does that qDebug() print ?

    Also why call QApplication::clipboard()->text() so many times ? You already have your QFileInfo object that contains the information you need.

    On a side note, if the target already exists, link will return false (or at list it's what the link member function will do).



  • @SGaist
    same as what i put in

    "file:///home/shaber/Downloads/gh/NH.png" "/home/shaber/Downloads/NH.png"
    

    i cam up with this ,but no change , always broken file.

        QString o(QApplication::clipboard()->text());
        QString newPath;
        if (!curIndex.isDir()) newPath = curIndex.filePath();
        else newPath = ui->pathEdit->text();
    
        QFileInfo f(o);
        QString a(f.fileName());
        QString sf(newPath+ "/" + a);
    
        qDebug() << o << sf ;
    
        QFile::link(o,sf);
    


  • @saber
    If your problem is really to do with the clipboard, putting in and showing us the output from a few judicious qDebug()s should help. Show the actual code and the actual output from qDebug() where you compare doing the same thing on identical paths with both literal strings in code and what you put on the clipboard.

    Meanwhile, also say what actually goes wrong. "Does not work" does not help us, and what does "using qfileinfo it creates a broken file." actually mean, what is a "broken file"? Don't you need to show us the output of ls -l on the symbolic link? Help us to help you....


  • Lifetime Qt Champion

    @saber said in qfile link function not working:

    "file:///home/shaber/Downloads/gh/NH.png" "/home/shaber/Downloads/NH.png"

    is not the same as:

    "/home/shaber/Downloads/gh/NH.png" "/home/shaber/Downloads/NH.png"

    file:/// makes it an URI. As I suggested before, use the path provided by QFileInfo.

    But before that, use QUrl::toLocalFile to get the proper path.

    In which case, and depending on what use you have of QFileInfo, you might not need it anymore.

    [edit: Fixed missing information SGaist]



  • @JonB

    @SGaist itried with file info ,not working

        QString o(QApplication::clipboard()->text());
        QString newPath;
        if (!curIndex.isDir()) newPath = curIndex.filePath();
        else newPath = ui->pathEdit->text();
    
        QFileInfo f(o);
        QString a(f.fileName());
        QString sf(newPath+ "/" + a);
    
        qDebug() << f.filePath() << sf ;
    
        QFile::link(f.filePath(),sf);
    

    here is the terminal output

    drwxr-xr-x 32 shaber shaber 4096 Nov 29 13:50  apps
    lrwxrwxrwx  1 shaber shaber   57 Nov 29 16:36 'Copy (1) of ju.m4a' -> 'file:///home/shaber/Desktop/testFolder/Copy (1) of ju.m4a'
    drwxr-xr-x  2 shaber shaber 4096 Nov 25 17:59  git
    drwxr-xr-x  4 shaber shaber 4096 Nov 29 13:38  manjaro
    drwxr-xr-x  4 shaber shaber 4096 Nov 25 15:24 'New Folder'
    -rw-r--r--  1 shaber shaber   53 Nov 27 11:32  task
    drwxr-xr-x  6 shaber shaber 4096 Nov 28 22:23  test
    drwxr-xr-x  6 shaber shaber 4096 Nov 28 22:34  testFolder
    -rwxr-xr-x  1 shaber shaber  136 Sep  9 12:10  Wallpaper.desktop
    
    

    here is debug output

    16:36:31: Starting /home/shaber/Desktop/apps/build-corefm-Desktop-Debug/corefm...
    qt5ct: using qt5ct plugin
    "file:///home/shaber/Desktop/testFolder/Copy (1) of ju.m4a" "/home/shaber/Desktop/Copy (1) of ju.m4a"
    

    here is a woking link created by drictly putting the boyh paths. actufile name is NH.p[ng

    total 32
    drwxr-xr-x 32 shaber shaber 4096 Nov 29 13:50  apps
    lrwxrwxrwx  1 shaber shaber   57 Nov 29 16:36 'Copy (1) of ju.m4a' -> 'file:///home/shaber/Desktop/testFolder/Copy (1) of ju.m4a'
    drwxr-xr-x  2 shaber shaber 4096 Nov 25 17:59  git
    drwxr-xr-x  4 shaber shaber 4096 Nov 29 13:38  manjaro
    drwxr-xr-x  4 shaber shaber 4096 Nov 25 15:24 'New Folder'
    lrwxrwxrwx  1 shaber shaber   32 Nov 29 16:41  NH.png -> /home/shaber/Downloads/gh/NH.png
    -rw-r--r--  1 shaber shaber   53 Nov 27 11:32  task
    drwxr-xr-x  6 shaber shaber 4096 Nov 28 22:23  test
    drwxr-xr-x  6 shaber shaber 4096 Nov 28 22:34  testFolder
    -rwxr-xr-x  1 shaber shaber  136 Sep  9 12:10  Wallpaper.desktop
    
    

    @JonB not working means, the file is not recognizeed as the typeit is originaly.system thinks it is a unknone file type



  • @saber
    It's "not working" because what you have the link pointing to begins with file://. That means nothing to a Linux filing system. You need it pointing to /home/shaber/Desktop/testFolder/Copy (1) of ju.m4a (presumably). (I see @SGaist has already drawn your attention to this.)



  • @JonB
    i notice that .but i can't find the way to get only the file path from qapplication::clipboard



  • @saber
    @SGaist previously said:

    file:/// makes it an URI. As I suggested before, use the path provided by QFileInfo.


  • Lifetime Qt Champion

    My answer was incomplete. Since you know it's an URI, use QUrl::toLocaleFile and you'll have the path you need.

    I've updated my previous answer.



  • @SGaist

    it's working now.

        QString o(QApplication::clipboard()->text());
        QString newPath;
        if (!curIndex.isDir()) newPath = curIndex.filePath();
        else newPath = ui->pathEdit->text();
    
        QFileInfo f(o);
        QString a(f.fileName());
        QString sf(newPath+ "/" + a);
    
        qDebug() << f.filePath() << sf ;
        QUrl l(f.filePath());
    
        QFile::link(l.toLocalFile(),sf);
    
    

Log in to reply