简体   繁体   English

如何在QFileSystemModel中更新文件权限

[英]How to update file permissions in a QFileSystemModel

Is there a way to update a file's permissions in QFileSystemModel (c++)? 有没有一种方法可以在QFileSystemModel(c ++)中更新文件的权限? Prior to allowing a user to rename a file listed in the model using a qtreeview, I make sure the file gets checked out of source control. 在允许用户使用qtreeview重命名模型中列出的文件之前,请确保已从源代码管理中检出该文件。 At this point the file is no longer read only, but the model still believes it's read only. 此时文件不再是只读的,但是模型仍然认为它是只读的。 How can I force the model to update a file's permissions without losing the expand / collapse state of the tree? 如何在不丢失树的展开/折叠状态的情况下强制模型更新文件的权限?

Thanks! 谢谢!

Update: The file is already flagged as writeable after checking out the file. 更新:检出文件后,该文件已被标记为可写。 The Model remains unaware of the change though. 模型仍然不知道更改。

QFile file(path.c_str()); 
QFileDevice::Permissions perms = file.permissions(); 
if (perms & QFile::WriteUser) 
{ 
   // Is already true 
} 

Just to be sure, I went ahead and used 可以肯定的是,我继续使用

file.setPermissions(file.permissions() | QFile::WriteUser); 

with no luck changing the permissions reported for that file in the model. 不能更改模型中为该文件报告的权限。

Update: 更新:

int perms = fsModel->data(index, QFileSystemModel::Roles::FilePermissions).value<int>();
if (perms & QFile::WriteUser)
{
    int i = 0;
}

Note: the above permissions never has the QFile::WriteUser flag set unless the file was writeable before the model was created. 注意:除非创建模型之前文件是可写的,否则上述权限永远不会设置QFile :: WriteUser标志。

setRootPath() is the key to solving this as well. setRootPath()也是解决此问题的关键。 It seems that you have to call it twice to get it to update the read only permissions. 似乎您必须调用它两次才能获取它以更新只读权限。 I stumbled across this when I changed my selection code to call: 当我将选择代码更改为call时,我偶然发现了这一点:

m_pFileModel->setRootPath("");
m_pFileModel->setRootPath(path.c_str());

everytime an item was selected. 每次选择一个项目。 Then when I doubleclicked an item, I saw the icon change to checked out. 然后,当我双击一个项目时,我看到图标已更改为检出。 Granted it didn't immediately let me rename it, I had to double click it again, but it does work. 承认它并没有立即让我重命名,因此我不得不再次双击它,但是它确实起作用。

My Process: 我的流程:

Connect to the OnBeginEdit() signal and checkout the file / change permissions 连接到OnBeginEdit()信号并签出文件/更改权限

When an item is selected: 选择一个项目时:

m_pFileModel->setRootPath("");
m_pFileModel->setRootPath(path.c_str());

Inside OnBeginEdit() 内在OnBeginEdit()

Do the following TWICE if you didn't set the path to the current folder when the item was selected 如果在选择项目时未将路径设置为当前文件夹,请执行以下两次

m_pFileModel->setRootPath("");
m_pFileModel->setRootPath(path.c_str());

Keep in mind you will have to doubleclick twice or press F2 twice - once to checkout and second to actually change the file. 请记住,您将不得不双击两次或按F2两次-一次签出,第二次实际更改文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM