Qt - 如何使用 QFileDialog 复制 QFile::copy 的文件?
Qt - How to copy file with QFile::copy using QFileDialog?
QString filename = QFileDialog::getOpenFileName(this,tr("Pdf files"), "C:/", "books(*.pdf)");
我想从 QFileDialog
获取选定的文件并将其复制到我的桌面。我可以使用这样的东西吗?
QFile::copy(filename,"desktop");
您需要使用 QStandardPaths
获取桌面路径,然后在调用 QFile::copy
时使用该路径。
假设您想在复制时保留文件名,您的代码将如下所示:
QString filePath = QFileDialog::getOpenFileName(this ,
QObject::tr("Pdf files"),
"C:/", "books(*.pdf)");
QFileInfo fi(filePath);
QString fileName= fi.fileName();
QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
QString destinationPath= desktopPath+QDir::separator()+fileName;
if(QFile::copy(filePath, destinationPath))
qDebug() << "success";
else
qDebug() << "failed";
QString filename = QFileDialog::getOpenFileName(this,tr("Pdf files"), "C:/", "books(*.pdf)");
我想从 QFileDialog
获取选定的文件并将其复制到我的桌面。我可以使用这样的东西吗?
QFile::copy(filename,"desktop");
您需要使用 QStandardPaths
获取桌面路径,然后在调用 QFile::copy
时使用该路径。
假设您想在复制时保留文件名,您的代码将如下所示:
QString filePath = QFileDialog::getOpenFileName(this ,
QObject::tr("Pdf files"),
"C:/", "books(*.pdf)");
QFileInfo fi(filePath);
QString fileName= fi.fileName();
QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
QString destinationPath= desktopPath+QDir::separator()+fileName;
if(QFile::copy(filePath, destinationPath))
qDebug() << "success";
else
qDebug() << "failed";