Qt C++ remove a read only file in windows using - c++

I have set a file to be read-only (right click and check readonly). Now when I try to remove the file using the function bool QDir::remove(const QString & fileName) the file is not removed and false is returned.
How do I proceed with this? I have tried fiddling around by changing the permission of the file using QFile::setPermission, but that returns false too.
Can anybody advise an approach for the same?

file.setPermissions(QFile::ReadOther | QFile::WriteOther);
file.remove();
should work.

You can set file permissions with QFile
Of course this only for files you have user permission to do. The error may also be because the file is open in another app

First, have you checked QFile::error() to see why the file wasn't removed?
Second, in the event that you're still not getting a useful error message back, you could check the source to find out if you can get more information. Checking the source reveals the following, for example:
QFile::remove() uses the underlying file engine to do the removal. That file engine is platform specific and in qfsfileengine_win.cpp for windows. Line 830 shows that it's using DeleteFile to do the removal so you might be able to get more information by calling GetLastError, though I'd hope that Qt translates the error message appropriately.

QDir::remove() function is not a static function. so you can create QDir with parent file path and call then remove it:
QDir dir(parent's directory);
and then
dir.remove(fileName);

I think you should use this:
bool QFile::remove ( const QString & fileName ) [static]
instead of this:
QDir::remove ( const QString & fileName )

Have you tried to use bool QFile::remove(const QString &fileName)?

Related

How to write filepath in ini's section with QSettings

I need to use filepath as a section name in ini file and I try to do it like this:
QSettings ini(iniPath, QSettings::IniFormat);
ini.beginGroup("C:\\Users\\Username\\Documents\\222.txt");
ini.setValue(attributeName, attributeValue);
...
ini.endGroup();
Then I try to read values from this section:
QSettings ini(iniPath, QSettings::IniFormat);
ini.beginGroup("C:\\Users\\Username\\Documents\\222.txt");
auto allKeys = ini.allKeys();
for (auto& key: allKeys)
{
QString val = ini.value(key);
...
}
ini.endGroup();
But in the output file I can see just "[C%3AUsers%5CUsername%5CDocuments%5C222.txt]" as name of section. And most importantly that QSettings can't find this section when reads ini file, allKeys variable is always empty.
What could be the problem? Thanks.
Look at the QSettings::Format documentation:
QSettings always treats backslash as a special character and provides no API for reading or writing such entries.
Use QDir::fromNativeSeparators() for the section name. When you read back, just use the path as is (all Qt classes support it) or covert it to the Windows-style with QDir::toNativeSeparators().
It was my mistake. The code in my question is a little bit simplified. Code for writing and reading is located in methods "save" and "load". I Feel myself rather silly :( The problem was in the difference between input parameters in "save" and "load" methods. In "save" method input string was "C:\Users\Username\Documents\222.txt" and in "load" method it was "C:/Users/Username/Documents/222.txt" so when I tried to read the settings in "load" method QSettings didn't can find the section.
As I understand it QSettings escapes special characters like "\" with "%" symbol. And "/" symbol QSettings uses for making nestings. So I decide to use paths like "C:\Users\Username\Documents\222.txt" as is. It works fine. Thank you all.

How to get the *full* filepath of a selected file via QFileSystemModel?

How to get a filepath via QFileSystemModel? Selecting via selection model returns just file name, or drive name.
Note, that just file name isn't enough. Whole filepath is needed.
On qt website: http://doc.qt.io/qt-5/qfilesystemmodel.html
QString QFileSystemModel::filePath ( const QModelIndex & index ) const
Returns the path of the item stored in the model under the index given.
I dont know if this answers your question, else you can give us some code about what you tried / are trying to reach.
You can do this by using something like this QFileSystemModel::filePath(index).

How to Remove the Readonly attribute of a File MFC

In my MFC application I have set the read only attribute on a particular file.
I have done this by using the SetFileAttributes() function.
At some point I have to remove that attribute of that file again.
Can anyone explain how to do this?
Use SetFileAttributes again to reset the flag:
SetFileAttributes( pszFilename,
GetFileAttributes(pszFilename) & ~FILE_ATTRIBUTE_READONLY);
Might be worth adding that this method returns 0 if the function fails and you can use GetLastError().

Qt QSettings try to create ini file but none created why?

Im trying to create ini file that will hold me the configuration data, I have singletone class
that setting the QSettings object like this :
... #DEFINE CONFIG_FILE_NAME "myconfig.ini"
m_pSettings = new QSettings(QDir::currentPath()+"/"+CONFIG_FILE_NAME,QSettings::IniFormat);
this is accourding the document, but when i look in my application dir, there is none myconfig.ini file created, what im doing wrong ?
I believe in order to force QSettings file to appear you would need to set at least one value in it and then call sync() method. See if an example below would work for you:
QSettings* settings = new QSettings(QDir::currentPath() + "/my_config_file.ini", QSettings::IniFormat);
settings->setValue("test", "value");
settings->sync();
hope this helps, regards
I dont think that "/"+CONFIG_FILE_NAME return the expected result. May be the cause of your problem..
Anyway operator +() is present in QString class so QDir::currentPath() + "/my_config_file.ini" must work fine.

Qt4.4 how to get the user settings path

linux: $HOME/.config
windows: %APPDATA%
mac os: $HOME/.config
It can be set using http://qt-project.org/doc/qt-4.8/qsettings.html#setPath, but it seems as I am not able to retrieve it.
http://qt-project.org/doc/qt-4.8/qlibraryinfo.html#location QLibraryInfo::LibrariesPath returns the system wide settings dir, which is not what I want.
Any ideas, or do I have to code it separately for each platform?
€: I want to create a sub directory, and store files into it. (You may punish me if this is a bad idea)
This might not answer your question directly: if you want to store per-user persistent data, shouldn't you use QDesktopServices::storageLocation(QDesktopServices::DataLocation) instead?
This is a nasty workaround. First you create QSettings, then get its location.
QSettings cfg(QSettings::IniFormat, QSettings::UserScope,
"organization", "application");
QString config_dir = QFileInfo(cfg.fileName()).absolutePath() + "/";
Credits go to the Qt Centre forum.
QSettings stores the default config in the user AppData directory. See documentation for QSettings. Also this code instructs to store the config in the Ini file format.
this works on both qt 4 and qt 5
QApplication::setApplicationName("MyApp");
QApplication::setOrganizationName("Me");
QString homePath;
#if QT_VERSION >= 0x050000
homePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
#else
homePath = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
#endif
Why do you need to know the settings path? If you are going to put settings in it, you could use QSettings. I could see making a subdirectory to hold various settings, but it seems like the easiest way would be to use QSettings directly.
As far as I can tell, you can't retrieve the path. In the Qt source, src/corelib/io/qsettings.cpp, there is a function to get the path:
static QString getPath(QSettings::Format format, QSettings::Scope scope)
{
...
but it's not accessible from code using Qt. You can't copy it and use it either, because it uses internal Qt globals to store the path...
EDIT: A solution was posted, using QDesktopServices.storageLocation(QDesktopServices.DataLocation) but it doesn't do exactly what the question was asking for, i.e. if I set a custom path using QSettings.setPath() it doesn't reflect the change.
What platform are you at?
Might be related or not but in windows, the default is to write QSettings to the registry.
I read more into the question than there was as it was originally posted. It is clearer after the edits. Ok, so can't you use..
QString QSettings::fileName () const
Returns the path where settings are written to using this QSettings object are stored.
On Windows, if the format is QSettings::NativeFormat, the return value is a system registry path, not a file path.