QFile path's toNativeSeparators returns wrong value on separator
-
HI everyone
I'm trying to set a path with this function
void Save_DB::set_new_path(QString path)
{
new_path = QDir::toNativeSeparators(path);
if (!QFile(new_path).exists())
qDebug() << "file not exist!\n";
return;
}
and try to examine it with my own file.
I'm pretty sure that there's something wrong with tonativeSeparatorsmy input is
D:/XL_db.db
and the QFile.exists() returns false
I've printed it which shows
D:XL_db.dbany idea of convertion of different separators like '' '//' '/' '\' in win10 msvc2015 qt4.9?
-
@Puppy-Bear if you're using QFile, than there is no need for
toNativeSeparators
QFile operates on normal forwards slashes and handles those automatically correctly, when talking to system level functions/apis -
@Puppy-Bear said in Qt's toNativeSeparators returns wrong value on separator:
I've printed it which shows
How?
I'm pretty sure the issue is not QDir::toNativeSeparators. -
@Puppy-Bear said in QFile path's toNativeSeparators returns wrong value on separator:
@J-Hilk Thanks for the help,
I've searched and think i'm supposed to use '/' in windows,
but is it possible to make different separators converted to the right one?
espically when the path is given by a user?no, not when you're using Qt!
QFile is able to work with both, no problem at all.int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QFile fileFSlash("C:/Qt/Tools/QtCreator/bin/qtcreator.exe"); QFile fileBSlash("C:\\Qt\\Tools\\QtCreator\\bin\\qtcreator.exe"); qDebug() << fileFSlash.exists() << fileBSlash.exists(); //results in true and true }
-
@Puppy-Bear said in QFile path's toNativeSeparators returns wrong value on separator:
but is it possible to make different separators converted to the right one?
@J-Hilk is right that you don't have to convert to forward slashes. But you can use QDir::fromNativeSeparators() to normalize to forward slashes on all platforms.
Or go through QFileInfo - QFileInfo("C:\\dev") .filePath() will also return "C:/dev".
-
@Puppy-Bear said in QFile path's toNativeSeparators returns wrong value on separator:
any idea of convertion of different separators like '' '//' '/' '' in win10 msvc2015 qt4.9?
First, please note that Qt internally always uses
/
as path separator, event when running on Windows.
BUT you can useQDir::toNativeSeparators()
to convert a path to use the native notation.
This can be useful when you want to use the path with an external application.