是否有Qt函數將文件移動到回收站而不是真正刪除它們,對於支持它的操作系統,還是我需要使用操作系統特定的代碼?將文件移動到Qt中的垃圾桶/回收站
回答
目前還沒有API。
https://bugreports.qt.io/browse/QTBUG-181
問題被關閉,該修補程序的版本是:未來的版本
編輯:一個新的問題已經在https://bugreports.qt.io/browse/QTBUG-47703打開。
我認爲沒有跨平臺的方式。簡單地將文件移動到「垃圾」位置將不起作用,因爲用戶可能會關閉此可能性。
也許,這個網址可以幫助:http://www.hardcoded.net/articles/send-files-to-trash-on-all-platforms.htm
的製品是用於Python然而,不爲C++。 – sashoalm
@sashoalm這篇文章是關於使用API的。用什麼語言來稱呼他們並不重要。 –
我是比較肯定的是,沒有Qt的API一個包裝此對所有支持的平臺。這意味着,不幸的是,你將不得不編寫特定於平臺的代碼。
我不知道Linux發行版在何處/如何存儲刪除的文件,我想它可能會因使用的文件管理器而異。我相信將文件移動到~/.Trash
文件夾是執行此操作的標準方式,但我不確定這是否可靠。例如,在存儲在外部捲上的文件的情況下。
在Mac OS X上,有一個受支持的API可以執行此操作:在覈心服務中提供的FSMoveObjectToTrashSync
更容易一些。至少,我記得你應該這樣做。 The documentation聲稱此方法現在在OS X 10.8中已棄用。我不知道推薦的選擇是什麼。
作爲一名Windows程序員,我認爲該平臺要容易得多。 :-)基本的解決方法是調用SHFileOperation
功能:
#include <Windows.h> // general Windows header file
#include <ShellAPI.h> // for shell functions, like SHFileOperation
#include <string> // (or use QString)
void RecycleFileOnWindows()
{
std::wstring path = L"C:\\Users\\Administrator\\Documents\\deleteme.txt";
path.append(1, L'\0'); // path string must be double nul-terminated
SHFILEOPSTRUCT shfos = {};
shfos.hwnd = nullptr; // handle to window that will own generated windows, if applicable
shfos.wFunc = FO_DELETE;
shfos.pFrom = path.c_str();
shfos.pTo = nullptr; // not used for deletion operations
shfos.fFlags = FOF_ALLOWUNDO; // use the recycle bin
const int retVal = SHFileOperation(&shfos);
if (retVal != 0)
{
// The operation failed...
if (shfos.fAnyOperationsAborted)
{
// ...but that's because the user canceled.
MessageBox(nullptr, L"Operation was canceled", nullptr, MB_OK | MB_ICONINFORMATION);
}
else
{
// ...for one of the other reasons given in the documentation.
MessageBox(nullptr, L"Operation failed", nullptr, MB_OK | MB_ICONERROR);
}
}
}
也有可以設置自定義的確認,錯誤報告,和其他行爲的標誌。鏈接文檔包含您需要基於此基本示例構建的所有詳細信息。
在Windows Vista及更高版本中,SHFileOperation
功能已被接口IFileOperation
提供的方法所取代。如果您只針對這些更高版本的Windows,則應該更喜歡使用此界面。否則,SHFileOperation
將繼續正常工作。
在Linux上存在[FreeDesktop.org垃圾桶規格】(http://www.ramendik.ru/docs/trashspec.html),這似乎談論存儲在外部捲上的文件的情況。它受許多流行的Linux文件管理器支持,包括Nautilus和Dolphin。 – silviubogan
或者Qt的人可能會很務實,只是將它用於Ubuntu和未來1-2個最受歡迎的發行版。 – sashoalm
Qt不提供MoveToTrash。下面是我的代碼
一部分用於Windows
#ifdef Q_OS_WIN32
#include "windows.h"
void MoveToTrashImpl(QString file){
QFileInfo fileinfo(file);
if(!fileinfo.exists())
throw OdtCore::Exception("File doesnt exists, cant move to trash");
WCHAR from[ MAX_PATH ];
memset(from, 0, sizeof(from));
int l = fileinfo.absoluteFilePath().toWCharArray(from);
Q_ASSERT(0 <= l && l < MAX_PATH);
from[ l ] = '\0';
SHFILEOPSTRUCT fileop;
memset(&fileop, 0, sizeof(fileop));
fileop.wFunc = FO_DELETE;
fileop.pFrom = from;
fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;
int rv = SHFileOperation(&fileop);
if(0 != rv){
qDebug() << rv << QString::number(rv).toInt(0, 8);
throw OdtCore::Exception("move to trash failed");
}
}
#endif
,併爲Linux
#ifdef Q_OS_LINUX
bool TrashInitialized = false;
QString TrashPath;
QString TrashPathInfo;
QString TrashPathFiles;
void MoveToTrashImpl(QString file){
#ifdef QT_GUI_LIB
if(!TrashInitialized){
QStringList paths;
const char* xdg_data_home = getenv("XDG_DATA_HOME");
if(xdg_data_home){
qDebug() << "XDG_DATA_HOME not yet tested";
QString xdgTrash(xdg_data_home);
paths.append(xdgTrash + "/Trash");
}
QString home = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
paths.append(home + "/.local/share/Trash");
paths.append(home + "/.trash");
foreach(QString path, paths){
if(TrashPath.isEmpty()){
QDir dir(path);
if(dir.exists()){
TrashPath = path;
}
}
}
if(TrashPath.isEmpty())
throw Exception("Cant detect trash folder");
TrashPathInfo = TrashPath + "/info";
TrashPathFiles = TrashPath + "/files";
if(!QDir(TrashPathInfo).exists() || !QDir(TrashPathFiles).exists())
throw Exception("Trash doesnt looks like FreeDesktop.org Trash specification");
TrashInitialized = true;
}
QFileInfo original(file);
if(!original.exists())
throw Exception("File doesnt exists, cant move to trash");
QString info;
info += "[Trash Info]\nPath=";
info += original.absoluteFilePath();
info += "\nDeletionDate=";
info += QDateTime::currentDateTime().toString("yyyy-MM-ddThh:mm:ss.zzzZ");
info += "\n";
QString trashname = original.fileName();
QString infopath = TrashPathInfo + "/" + trashname + ".trashinfo";
QString filepath = TrashPathFiles + "/" + trashname;
int nr = 1;
while(QFileInfo(infopath).exists() || QFileInfo(filepath).exists()){
nr++;
trashname = original.baseName() + "." + QString::number(nr);
if(!original.completeSuffix().isEmpty()){
trashname += QString(".") + original.completeSuffix();
}
infopath = TrashPathInfo + "/" + trashname + ".trashinfo";
filepath = TrashPathFiles + "/" + trashname;
}
QDir dir;
if(!dir.rename(original.absoluteFilePath(), filepath)){
throw Exception("move to trash failed");
}
File infofile;
infofile.createUtf8(infopath, info);
#else
Q_UNUSED(file);
throw Exception("Trash in server-mode not supported");
#endif
}
#endif
if(QSysInfo::kernelType()=="linux")
{
QDateTime currentTime(QDateTime::currentDateTime()); // save System time
QString trashFilePath=QDir::homePath()+"/.local/share/Trash/files/"; // trash file path contain delete files
QString trashInfoPath=QDir::homePath()+"/.local/share/Trash/info/"; // trash info path contain delete files information
// create file format for trash info file----- START
QFile infoFile(trashInfoPath+FileName.completeBaseName()+"."+FileName.completeSuffix()+".trashinfo"); //filename+extension+.trashinfo // create file information file in /.local/share/Trash/info/ folder
infoFile.open(QIODevice::ReadWrite);
QTextStream stream(&infoFile); // for write data on open file
stream<<"[Trash Info]"<<endl;
stream<<"Path="+QString(QUrl::toPercentEncoding(FileName.absoluteFilePath(),"~_-./"))<<endl; // convert path string in percentage decoding scheme string
stream<<"DeletionDate="+currentTime.toString("yyyy-MM-dd")+"T"+currentTime.toString("hh:mm:ss")<<endl; // get date and time format YYYY-MM-DDThh:mm:ss
infoFile.close();
// create info file format of trash file----- END
QDir file;
file.rename(FileName.absoluteFilePath(),trashFilePath+FileName.completeBaseName()+"."+FileName.completeSuffix()); // rename(file old path, file trash path)
}
歡迎計算器。請解釋一下你的答案。 –
只要注意'QSysInfo :: kernelType()==「linux」',Android的內核也是Linux。 – sashoalm
在linux垃圾文件存在/home/user_name/.local/share/Trash/files/
目錄,但它也需要爲每個垃圾文件信息文件,該文件存在於/home/user_name/.local/share/Trash/info/
目錄中。當我們要文件移入回收站,實際移動文件到/home/user_name/.local/share/Trash/files/
目錄和/home/user_name/.local/share/Trash/info/
目錄中創建信息文件。裏面其中file存在了一套文件路徑.trashinfo格式使用百分比解碼方案,信息文件還包含時間和刪除的日期。
- 1. VBA:將垃圾郵件從垃圾郵件移動到收件箱
- 2. 拖動到垃圾桶jquery
- 3. 將文件移動到回收站(PHP)
- 4. 將目錄移至垃圾桶
- 5. qt垃圾回收和智能指針
- 6. 有沒有辦法使用cmd.exe或PowerShell將文件夾移動到垃圾桶?
- 7. 將郵件從收件箱移動到垃圾郵件文件夾的算法
- 8. Perl中的垃圾回收
- 9. iPhone垃圾桶吸動畫
- 10. 如何使用PyQt4將文件移動到不同平臺上的回收站/垃圾箱?
- 11. java垃圾回收
- 12. Java:垃圾回收
- 13. Python垃圾回收
- 14. C#垃圾回收
- 15. Erlang垃圾回收
- 16. java - 垃圾回收
- 17. JDBC垃圾回收
- 18. JS垃圾回收
- 19. requestAnimationFrame垃圾回收
- 20. vb.net垃圾回收
- 21. ColdFusion垃圾回收
- 22. PhoneGap垃圾回收
- 23. Chrome垃圾回收
- 24. Java:垃圾回收
- 25. Javascript垃圾回收
- 26. nodejs require()從文件垃圾回收JSON
- 27. 移動垃圾收集器的成本
- 28. 區分回收站視圖中的拖動操作和垃圾回收操作
- 29. v8 |手動啓動垃圾回收器
- 30. java的垃圾回收Runnable
奇怪的問題處理。關閉爲...?不會修復?太難?通常,當答案是「也許以後」時,這個問題只是保持開放。 –
原因:超出範圍 – fjardon
有一個新的問題,在這裏重新打開這個老之一:https://bugreports.qt.io/browse/QTBUG-47703 – Felix