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).
Related
I would like to pass the settings saved with QSetting through two instances of an application, for example through a socket.
I could not see any function in the official documentation. The only thing I read is for example this post
Save Configuration Settings to XML file in QT?
But I do not want to save the settings in a XML file, for example in Windows I want to continue using the Registry.
I just want to collect all the settings, and pass them through a socket. And the receiver could check the settings and eventually substitute its own settings with the received ones.
Well, I suppose I could do something similar using QSettings::allKeys(), checking all the values, convert to strings, etc etc...but do you know if there is some native function in Qt already implemented?
Thanks to everyone in advance
Best solution that I found:
Create a QMap from QSettings
QMap<QString, QVariant> keysValuesPairs;
QStringList keys = settings.allKeys();
QStringListIterator it(keys);
while ( it.hasNext() )
{
QString currentKey = it.next();
keysValuesPairs.insert(currentKey, settings.value(currentKey));
}
And then write it in a QJson with the function (see the official documentation http://doc.qt.io/qt-5/qjsonobject.html)
QJsonObject::fromVariantMap
then in the other side recover it with
QJsonObject::toVariantMap()
and rewrite the settings
for ( int i = 0; i < keys.size(); i++ )
{
settings.setValue( keys.at(i), keysValuesPairsMap.value(keys.at(i)) );
}
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.
I ask the above question as whenever I use this to determine the person who last saved a file it just returns a blank string. Am i doing it wrong or is there an equivalent library/method that will do this for me?
Forgot to add my code. It's simply.
QFileInfo fileName = it.fileInfo();
qDebug() << fileName.owner();
And here's what Qt says about ownerId() :
uint QFileInfo::ownerId() const
Returns the id of the owner of the
file.
On Windows and on systems where files do not have owners this function
returns ((uint) -2).
Probably you are doing sth wrong. For owner() function qt site says:
QString QFileInfo::owner () const
Returns the owner of the file. On systems where files do not have
owners, or if an error occurs, an empty string is returned.
This function can be time consuming under Unix (in the order of
milliseconds).
This method should return account name for owner of the file. You have to do sth else to get the name who edited file last.
Edit: QFileInfo::lastModified () maybe useful for you.
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.
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)?