Is there a cross-platform function in Qt that is equivalent to the MoveFile function in Windows and the mv command in Linux?
You have to first create the directory ( QDir::mkpath will create the full path) and then the file ( QFile::open ).
abk file to load it into the address book. void AddressBook::loadFromFile() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open Address Book"), "", tr("Address Book (*. abk);;All Files (*)")); On Windows, for example, this function pops up a native file dialog, as shown in the following screenshot.
To write text, we can use operator<<(), which is overloaded to take a QTextStream on the left and various data types (including QString) on the right: QFile file("out. txt"); if (! file.
It is also better use QFile::rename(const QString & oldName, const QString & newName) to rename a file. When using QDir::rename, If oldName is a file (not a directory) that can't be renamed right away, Qt will try to copy oldName to newName and remove oldName.
Sure, QDir::rename()
following the old Unix / POSIX tradition of calling this rename.
Which makes sense if you think of a file with its complete path: the underlying inodes just get assigned a different path/file label.
You would use QDir::rename() but be beware of the special cases when rename()
can fail:
On most file systems,
rename()
fails only ifoldName
does not exist, ifnewName
andoldName
are not on the same partition or if a file with the new name already exists. However, there are also other reasons whyrename()
can fail. For example, on at least one file systemrename()
fails ifnewName
points to an open file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With