Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将文件移动到Qt中的垃圾桶/回收站_C++_Qt_Cross Platform_Recycle Bin - Fatal编程技术网

C++ 将文件移动到Qt中的垃圾桶/回收站

C++ 将文件移动到Qt中的垃圾桶/回收站,c++,qt,cross-platform,recycle-bin,C++,Qt,Cross Platform,Recycle Bin,对于支持Qt的操作系统,是否有Qt功能将文件移动到回收站而不是真正删除它们,或者我需要使用操作系统特定的代码?我认为没有跨平台的方法。简单地将文件移动到“垃圾箱”位置不会产生效果,因为用户可能会关闭这种可能性 也许,这个url会有所帮助:我相对确定,没有任何QtaPI可以为所有受支持的平台包装这个。不幸的是,这意味着您必须编写特定于平台的代码 我不知道Linux发行版在何处/如何存储删除的文件,我想它可能会因您使用的文件管理器而异。我相信将文件移动到~/.Trash文件夹是标准的方法,但我不确定

对于支持Qt的操作系统,是否有Qt功能将文件移动到回收站而不是真正删除它们,或者我需要使用操作系统特定的代码?

我认为没有跨平台的方法。简单地将文件移动到“垃圾箱”位置不会产生效果,因为用户可能会关闭这种可能性


也许,这个url会有所帮助:

我相对确定,没有任何QtaPI可以为所有受支持的平台包装这个。不幸的是,这意味着您必须编写特定于平台的代码

我不知道Linux发行版在何处/如何存储删除的文件,我想它可能会因您使用的文件管理器而异。我相信将文件移动到
~/.Trash
文件夹是标准的方法,但我不确定这是否可靠。例如,对于存储在外部卷上的文件

在Mac OS X上,事情会简单一些,因为有一个受支持的API来实现这一点:
fsmoveobjecttotrashync
,由核心服务提供。至少,我记得你应该这么做。声称此方法现在已在OS X 10.8中弃用。我不知道推荐的替代方案是什么

作为一名Windows程序员,我认为该平台要容易得多。:-)基本解决方案是调用函数:

#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头文件
#包括//用于shell函数,如SHFileOperation
#包括/(或使用QString)
void循环使用Windows()
{
std::wstring path=L“C:\\Users\\Administrator\\Documents\\deleteme.txt”;
append(1,L'\0');//路径字符串必须以双nul结尾
SHFILEOPSTRUCT shfos={};
shfos.hwnd=nullptr;//将拥有生成的窗口的窗口句柄(如果适用)
shfos.wFunc=FO_DELETE;
shfos.pFrom=path.c_str();
shfos.pTo=nullptr;//不用于删除操作
shfos.fFlags=FOF_ALLOWUNDO;//使用回收站
const int retVal=SHFileOperation(&shfos);
如果(返回值!=0)
{
//操作失败。。。
if(shfos.FanyoOperationsBorted)
{
//…但那是因为用户取消了。
消息框(nullptr,L“操作已取消”,nullptr,MB|u OK | MB|u图标信息);
}
其他的
{
//…由于文件中给出的其他原因之一。
消息框(nullptr,L“操作失败”,nullptr,MB|u OK | MB|u icon);
}
}
}
您还可以设置一些标志来自定义确认、错误报告和其他行为。链接的文档包含在这个基本示例基础上构建所需的所有详细信息

在Windows Vista及更高版本上,界面提供的方法取代了
SHFileOperation
功能。如果您只针对这些更高版本的Windows,您应该更喜欢使用此界面。否则,
SHFileOperation
将继续正常工作。

还没有API

问题已解决,修复版本为:某个未来版本

编辑:已在打开一个新问题


编辑显然,它现在是在5.15.0 Alpha
boolqfile::moveToTrash()中完成的
Qt不提供moveToTrash。这是我代码的一部分

窗户

#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
#ifdef Q#u OS#u WIN32
#包括“windows.h”
void MoveToTrashImpl(QString文件){
QFileInfo文件信息(文件);
如果(!fileinfo.exists())
抛出OdtCore::异常(“文件不存在,不能移动到垃圾箱”);
来自[最大路径]的WCHAR;
memset(from,0,sizeof(from));
int l=fileinfo.absoluteFilePath().towcharray(from);
Q_断言(0
if(QSysInfo::kernelType()==“linux”)
{
QDateTime currentTime(QDateTime::currentDateTime());//保存系统时间
QString trashFilePath=QDir::homePath()+“/.local/share/Trash/files/”;//垃圾文件路径包含删除文件
QString trashInfoPath=QDir::homePath()+“/.local/share/Trash/info/”;//垃圾信息路径包含删除文件信息
//创建垃圾信息文件的文件格式------开始
QFile infoFile(trashInfoPath+FileName.completeBaseName()+“+”+FileName.completeSuffix()+“.trashinfo”);//文件名+扩展名+.trashinfo//在/.local/share/Trash/info/文件夹中创建文件信息文件
open(QIODevice::ReadWrite);
QTextStream(&infoFile);//用于在打开的文件上写入数据

流linux中存在垃圾文件
/home/user\u name/.local/share/Trash/files/
目录,但它也要求存在于
/home/user\u name/.local/share/Trash/info/
目录中的每个垃圾文件都有信息文件。当我们想要将文件移动到垃圾桶中时,实际上是将文件移动到/home/user\u name/.local/share/Trash/files中/目录并在
/home/user\u name/.local/share/Trash/info/
目录中创建信息文件。在目录内部。trashinfo格式使用百分比解码方案设置文件存在的文件路径,信息文件还包含删除的时间和日期。

Qt 5.15.0 Alpha以来,第已经添加了is方法,这应该是您正在寻找的方法

bool QFile::moveToTrash()
可以找到相应的代码更改


(这个问题很老了,相应的Bugreport在已经发布了,但我目前缺乏评论的声誉,并且发现这是一个有用的信息。)

在Linux上,有一个似乎在谈论存储在外部卷上的文件的情况。它受到许多流行的Linux文件管理器的支持,包括Nautilus和Dolphin。或者Qt的家伙可能是务实的,只是在Ubuntu和下一个1-2最流行的发行版上实现它。奇怪的问题处理。关闭为…?不会修复?太难?通常很抱歉,当答案是“以后可能”时,这个问题就没有解决。这里有一个新问题重新打开了这个旧问题:更新:移动到垃圾箱
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)



}
bool QFile::moveToTrash()