Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move files in Qt?

Tags:

c++

qt

Is there a cross-platform function in Qt that is equivalent to the MoveFile function in Windows and the mv command in Linux?

like image 837
sashoalm Avatar asked Feb 06 '11 21:02

sashoalm


People also ask

How do you send a file path in Qt?

You have to first create the directory ( QDir::mkpath will create the full path) and then the file ( QFile::open ).

How do I load a file in Qt?

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.

How do you write to a file Qt?

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.

How do I rename a file in Qt?

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.


2 Answers

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.

like image 184
Dirk Eddelbuettel Avatar answered Oct 07 '22 06:10

Dirk Eddelbuettel


You would use QDir::rename() but be beware of the special cases when rename() can fail:

On most file systems, rename() fails only if oldName does not exist, if newName and oldName are not on the same partition or if a file with the new name already exists. However, there are also other reasons why rename() can fail. For example, on at least one file system rename() fails if newName points to an open file.

like image 30
Palmik Avatar answered Oct 07 '22 06:10

Palmik