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";
关于c++ - Qt - 如何使用 QFile::copy 使用 QFileDialog 复制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39169530/