Linux和OSX待测试:原生文件对话框的详细说明
因为我在开发第三方百度网盘客户端,所以也需要这个功能。
但是Qt自身可能难以实现,我看过Qt相关源代码,好像是用的Qt自己的对话框,也就是模拟出来的。
当然,前提是使用系统原生对话框,而不是Qt自己的文件对话框,也就是不使用
QFileDialog::DontUseNativeDialog
这个属性。
以下是Qt自带文档的说明:
QString QFileDialog::getExistingDirectory
On and OS X, this will use the file and not a . , the file does not files in the . You need to pass to files using a . On CE, if the has no file , a will be used.
也就是说原生文件对话框在只显示文件夹时是不支持同时显示文件的,我感觉这是Qt自身的bug,但我在Linux测试过,发现它调用的也是系统对话框,具体情况还需要进一步测试。
我对这个问题数天,目前没有找到真正能用的方法,暂时是用
: how to File or in the same
这里的代码:
/*解决Windows下系统原生文件对话框不能同时选择文件或者文件夹的问题
* QFileDialog::FileMode = QFileDialog::Directory说的是:
* The name of a directory. Both files and directories are displayed. However, the native Windows file dialog does not support displaying files in the directory chooser.
* 其实这个应该是Qt自己的问题,因为它把本地和远程选择的文件/文件夹路径作为QUrl处理。。。C#等语言都支持。。。不信看Qt源代码的qfiledialog.cpp文件
* (Qt5.6)位置:Qt源文件目录/qtbase/src/widgets/dialogs/qfiledialog.cpp void QFileDialog::accept()这里*/
BUFileDialog(QWidget * parent, Qt::WindowFlags flags)
: QFileDialog(parent,flags){}
BUFileDialog(QWidget * parent = 0, const QString & caption = QString(), const QString & directory = QString(), const QString & filter = QString()):
QFileDialog(parent, caption, directory, filter){}
void BUFileDialog::accept()
{
//在选择文件夹时返回选择的文件/文件夹列表
QStringList files = selectedFiles();
if (files.isEmpty())
{
return;
}
emit filesSelected(files);
QDialog::accept();
}
在下面确实可以看到文件和目录了,但是在不选择文件夹的情况下才能用,不能同时选择文件和目录。。。Linux和OSX待测试。
目前看来在最好是用来间接实现这个功能,其他平台待测试。
等过几天我测试完再补充答案。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。