How to update file permissions in a QFileSystemModel - c++

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.

Related

QFile: directory not found

I need to write a console application that takes a file, it opens it, and then it calls another procedure based on the information inside the text file.
The only problem is that QFile::errorString() returns:
No such file or directory.
I have been using this implementation in all the programs I had to, and yes, the file exists at that directory.
The code is:
QFile fileName("D:/file.txt");
QString read_from_file;
if(fileName.open(QIODevice::ReadOnly)){
QTextStream in(&fileName);
while(!in.atEnd())
{
read_from_file = in.readLine();
qDebug()<<read_from_file;
}
fileName.close();
}
qDebug()<<fileName.errorString();
Make sure that the file really exists.
QFile::exists("D:/file.txt") – This will return true if the file exists.
QDir("D:/").entryList() – This will return the list of the files and directories located at the specified path; the needed file should be in the list.
As you pointed out in the comments, the problem was the hidden file extensions on Windows.
Open Folder Options by clicking the Start button, clicking Control Panel, clicking Appearance and
Personalization, and then clicking Folder Options.
Click the View tab, and then Advanced settings <...>
To show file name extensions, clear the Hide extensions for known file
types check box, and then click OK.

Displaying picture that is not in resources of Qt

I am currently writing an application in Qt, which is basically a warehouse. An application reads CSV, enables user to process it and enables to show picture of each good. I tried displaying picture using QLabel and Pixmap, however nothing happens even though the file is in the same folder and the name provided is exactly as it should be. Is it the resources issue or my code fails somehow? Is there any possibility to display the image without adding it to resources in order to avoid adding many photos manually?
void ImageViewer::viewImage(QString imgName)
{
QString pathWithName = imgName;
pathWithName.append(".jpg");
ui->label->setPixmap( QPixmap(pathWithName) );
ui->label->show();
update();
}
Sorry for any mistakes in post creation or code displaying here- it's my first post.
Edit:
I am adding code from MainWindow (called CsvReader in my project) to how I'm invoking the method viewImage:
void CsvReader::on_imgView_clicked()
{
ImageViewer* img = new ImageViewer(this);
img->setModal(true);
img->exec();
QModelIndex List selInd ui->tableView->selectionModel()->selectedIndexes();
QString id = model->item(selInd.first().row(), 0)->text();
img->viewImage(id);
}
Edit 2:
Solved. Had to change path using QDir:
QDir* directory = new QDir("/home/kokos/Magazyn/photos");
QFileInfo checkFile(*directory, pathWithName);
Thanks in advance,
Kokos
Confirm your file's location and existence first. Add this;
QFileInfo checkFile(pathWithName);
if (checkFile.exists() && checkFile.isFile()) {
// your code
}

Filtering based on file extension not working in QFileSystemModel?

I am trying to filter file system model to show only files with extension .ncr (NCReport templates). The view instead shows all files. Any ideas how to make the filtering work? Thanks.
(I realize there's plenty other clumsiness here, suggestions welcome.)
fsmodel = new QFileSystemModel(this);
connect(fsmodel,SIGNAL(rootPathChanged(QString)),this,SLOT(fileSystemModelRootSetSuccessfully()));
// this call is required on windows to show anything in file view
QModelIndex rootIndex=fsmodel->setRootPath(reportdirstring);
// root index could be removed
Q_UNUSED(rootIndex);
fsmodel->setReadOnly(true);
ui->reportTemplateDirView->setModel(fsmodel);
ui->reportTemplateDirView->setRootIndex(fsmodel->index(reportdirstring));
ui->reportTemplateDirView->expandAll();
ui->reportTemplateDirView->header()->hide();
// selecting the first file entry with selectFileInDirView(); requires the qtreeview to be sorted
// sort in desc order since that is the only way to get the first item selected?
ui->reportTemplateDirView->sortByColumn(0,Qt::AscendingOrder);
fsmodel->sort(0,Qt::AscendingOrder);
QStringList filters;
filters << "*.ncr";
fsmodel->setNameFilters(filters);
fsmodel->setNameFilterDisables(false);
// hide report template directory view extra columns,
//type?
ui->reportTemplateDirView->setColumnHidden(1,true);
//size?
ui->reportTemplateDirView->setColumnHidden(2,true);
//date
ui->reportTemplateDirView->setColumnHidden(3,true);
#if QT_VERSION >= 0x040700
// as soon as QFileSystemModel has parsed the entire directory tree, tell the QTreeView to expand its hierarchy
// note that if there are a lot of files, this could be too inefficient.
// if problems arise, consider commenting this out or using a QDirModel, which could be equally inefficient though.
connect(fsmodel,SIGNAL(directoryLoaded(QString)),ui->reportTemplateDirView,SLOT(expandAll()));
connect(fsmodel,SIGNAL(directoryLoaded(QString)),this,SLOT(selectFileInDirView()));
#endif
// show a fake folder name + icon at the top of the folder tree of report template directory
QFileIconProvider iconProvider;
QIcon folderIcon=iconProvider.icon(QFileIconProvider::Folder);
ui->reportTemplatesLabel->setPixmap(QPixmap(folderIcon.pixmap(QSize(16,16))));
As Alexander noted, this actually works. The issue was that I was setting the filters to empty elsewhere. ^.^

QFileSystemModel and QTreeView - strange behavior when resetting view

I wrote this on official forums of Qt, but it seems dead, so I am going to copy-paste it here.
I am writing small program for copying files. I use QTreeView and I have inherited from QFileSystemModel, so I was able to add checkboxes to every row in the QTreeView. I also use setNameFilters method connected with QLineEdit, so user can specify what file extensions he wants to display in the QTreeView. I have spotted the following behavior:
1) When I run the program and enter extensions to filter (without touching any node from the QTreeView) everything works fine and files with extensions I have provided are only displayed (and folders of course). When I change the extensions and the view is refreshed, on my "C:/" drive everything is updated and only new set of extensions is displayed. When I expand some other drive that I didn’t touch before, it also shows files correctly.
2) When I run the program and expand let say my "C:/" and "D:/" drives I see all directories and files (expected behavior). Then I write some extensions and the view is refreshed. I expand "C:/" drive and everything works fine, only files with extensions I have provided are displayed. Then I go to "D:/" drive and here is the problem. It displays all files. It ignores the filters I have provided. When I open the "E:/" drive that I have not opened before, the files are filtered correctly as in "C:/" drive.
I have concluded, that this behavior has something to do with setRootPath method, because for my QTreeView only in "C:/" drive the filters are working correctly. All other drives that were expanded before change of filters don’t work. Those not expanded work just fine.
The question is: How to get this working, so after user changes the filters and reset() method is fired, the whole QTreeView is refreshed and not only root path and not-expanded elements? Maybe there exists some root path that have all the drives as children and it will work as expected? Or maybe I should make some virtual folder in the QTreeView called "MyComputer" and set it to be a parent for all the drives? But how to get list of all the available drives?
I hope that what I wrote is clear for you and you can help me to get this working.
Edit:
Adding some code that is relevant. If you need more just ask.
//setting up the model and view
QString rPath = "C:/";
rTree_model = new TreeModel(this); //TreeModel inherits from QFileSystemModel
rTree_model->setRootPath(rPath);
ui->rTree->setModel(rTree_model); //applies the model for the qtreeview (ui->rTree)
//(...)
//action when extensions were provided by user
QString extensions = QString(ui->extensionBox->text()); //gets extensions provided by user
QStringList filters;
if(extensions.length() > 0) {
filters = extensions.split(";", QString::SkipEmptyParts); //splits extensions provided with ';' as separator
rTree_model->setNameFilters(filters); //applies filters
ui->rTree->reset(); //resets the view
}
Try changing your root path to My Computer instead of C:/. It seems to work with QFileSystemModel in Windows 7 x64 and Qt 4.8.2, but I can't guarantee anything for other platforms.
rTree_model = new TreeModel(this);
QString rPath = model->myComputer().toString(); //causes the QFileSystemWatcher to watch every drive?
rTree_model->setRootPath(rPath);
ui->rTree->setModel(rTree_model);

Infragistics UltraDockManager: Resetting Panes back to original locations/dock states

We have a requirement to allow the user to customize (position/resize) their panes as they see fit. We also have a requirement that the user be able to reset the panes back to original (First Run) state. I don't see anything in the ultradockmanager that allows you to reset to original state. Any advice?
(Using NetAdvantage 12.2 Win CLR4x)
I don't know for sure if this could help you, but I put here as an answer.
Let me know if this doesn't resolve your problem.
The UltraDockManager has two methods called LoadFromXml and SaveAsXml that save and load the Layout of the control.
You could use SaveAsXml at startup of your form saving somewhere the initial layout, and, when required, call the LoadFromXml to reset the layout at the initial state.
So, for example, to save your layout
string userDataFolder=Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fileToSave=Path.Combine(userDataFolder, "MyAppDataFolder", "currentLayout.xml");
ultraDockManager1.SaveAsXML(fileToSave);
And for resetting the layout
string userDataFolder=Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fileToLoad=Path.Combine(userDataFolder, "MyAppDataFolder", "currentLayout.xml");
ultraDockManager1.LoadFromXML(fileToLoad);
Of course I suppose you have a folder (MyAppDataFolder) for your application inside the ApplicationData folder where you store application-specific data for the current user.