QString filename = QFileDialog::getOpenFileName(this,tr("Pdf files"), "C:/", "books(*.pdf)");
我想从QFileDialog
获取所选文件并将其复制到我的桌面。我可以使用这样的东西吗?
QFile::copy(filename,"desktop");
答案 0 :(得分:1)
您需要使用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";