简体   繁体   中英

How to update file permissions in a QFileSystemModel

Is there a way to update a file's permissions in 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. 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.

setRootPath() is the key to solving this as well. 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:

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

When an item is selected:

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

Inside 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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