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

Rename desktop file icon issue



  • Hi! I want to rename the shortcut for the some program. I have tried it both ways, but it fails (always returns false) and file is still with old name.

    Code:

    QDir myDir;
    myDir.setPath(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).replace("/", "\\"));
    qDebug() << myDir.rename("Test.lnk", "Test (1).lnk");
    
    QDir myDir;
    myDir.setPath(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).replace("/", "\\"));
    qDebug() << QFile::rename(myDir.path() + "\\Test.lnk", myDir.path() + "\\Test (1).lnk");
    

    I run a few test and it renames files in different locations, but not in QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).

    Any ideas how to rename the file in the desktop location? Thanks.



  • I have fixed it by using Win API (MoveFile function):

    Code:

    QDir myDir;
    myDir.setPath(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).replace("/", "\\"));
    
    QString oldName = myDir.path() + "\\Test.lnk";
    QString newName = myDir.path() + "\\Test (1).lnk";
    bool renameRes = MoveFile(oldName.toStdWString().c_str(), newName.toStdWString().c_str());
    
     if (renameRes) {
          qDebug() << "File has been renamed!";
     } else {
          qDebug() << "Failed to rename the file!";
     }
    

    Now it works well. The issue is resolved.


Log in to reply