我需要帮助,我一直在研究,直到现在一无所获,我在QT C ++中创建一个应用程序,允许用户共享文件(.mp3,jpg和.txt),如何从tableview获取文件将它存储在文件系统中?
// getting file from DB
QByteArray fileByteArray = dbm.GetSharedFile(fileNameSelected);
QFileDialog dialog;
dialog.setOptions(QFileDialog::DontUseNativeDialog); // with or without this
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setDirectory(".");
dialog.selectFile(fileNameSelected); // magic happens here
int ret = dialog.exec();
if (ret == QDialog::Accepted)
{
QFile f(dialog.directory ().absolutePath());
f.open( QIODevice::WriteOnly );
f.write(fileByteArray);
????
???
}
我可以从tableview获取文件,但之后,我不知道如何使用qfiledialog保存它。祝福
答案 0 :(得分:0)
QFileDialog
班级有静态成员:
QString QFileDialog::getSaveFileName(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0)
使用此方法获取文件名后,您应该检查字符串是否为空,然后像您一样使用文件
示例:
QString fname =QFileDialog::getSaveFileName(); //you can set some params
if (!fname.isEmpty()) {
QFile f(fname);
if(f.open(QIODevice::WriteOnly))
f.write(fileByteArray);
}
答案 1 :(得分:0)
就像你可以看到文件字节数组不是空的......我可能做错了......
......
......
qDebug() << "row selected" << rowSelected;
qDebug() << "file name to download" << fileNameSelected;
QByteArray fileByteArray = dbm.GetSharedFile(fileNameSelected);
qDebug() << fileByteArray.size(); // Checking the file byte array size
QFileDialog dialog;
QString fname =QFileDialog::getSaveFileName(); //you can set some params
if (!fname.isEmpty()) {
QFile f(fname);
if(f.open(QIODevice::WriteOnly))
f.write(fileByteArray);
}
qDebug() << fileByteArray.size(); // file size
}