简体   繁体   English

选择一个文件夹后QFileDialog :: getExistingDirectory没有关闭

[英]QFileDialog::getExistingDirectory does not close after choosing a folder

In Qt, 在Qt中

QFileDialog *dlg = new QFileDialog(); 
QDir dir = dlg->getExistingDirectory(this, tr("Choose folder"), qgetenv("HOME"));

opens a folder choose dialog. 打开一个文件夹选择对话框。 Once I select a folder (press choose button) the folder is not closing automatically. 选择文件夹(按“选择”按钮)后,该文件夹不会自动关闭。 So I tried: 所以我尝试过:

if(dlg->close() == true) delete(dlg);

When I debug the dlg->close() returns true and the code delete(dlg) is hit. 当我调试时,dlg-> close()返回true,并且命中了代码delete(dlg)。 Still the Folder chooser dialog box is not closing. 文件夹选择器对话框仍然没有关闭。

I am using Ubuntu 11.10 64 bit OS. 我正在使用Ubuntu 11.10 64位操作系统。 Using Qt libraries from the repository. 从存储库中使用Qt库。

My ultimate aim is just to show a folder chooser dialog and once the folder is chosen the dialog should close. 我的最终目的只是显示一个文件夹选择器对话框,选择文件夹后,该对话框应关闭。 After that processing should continue. 之后,处理应继续。 How to do this? 这个怎么做?

Thanks in advance. 提前致谢。

Even if QFileDialog::getExistingDirectory is static and doesn't need a QFileDialog object to work, it should close the dialog window when a directory is finally chosen. 即使QFileDialog::getExistingDirectory是静态的,并且不需要QFileDialog对象才能工作,当最终选择目录时,它也应该关闭对话框窗口。 By default that function tries to open a native file dialog window, which seems to cause some problems on some platforms. 默认情况下,该函数尝试打开本机文件对话框窗口,这在某些平台上似乎会引起一些问题。

You should try forcing a non-native dialog by adding the option DontUseNativeDialog : 您应该尝试通过添加选项DontUseNativeDialog强制使用非本机对话框:

QString dir = QFileDialog::getExistingDirectory(
    this, 
    tr("Choose folder"),
    QDesktopServices::storageLocation(QDesktopServices::HomeLocation),
    QFileDialog::ShowDirsOnly | QFileDialog::DontUseNativeDialog);

And remove the two other lines (with new QFileDialog and if(dlg->close()) ... ). 并删除其他两行(使用new QFileDialogif(dlg->close()) ... )。

getExistingDirectory(...)是静态函数。

To add to cmannett85's answer: 要添加到cmannett85的答案:

You should not make an instance of QDialog . 您不应该创建QDialog的实例。 If you do, it's up to you to hide it. 如果这样做,则由您自行隐藏。 Modify your code to read 修改代码以读取

const QString home = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
const QDir dir = QFileDialog:getExistingDirectory(this, tr("Choose folder"), home);

This code should be relatively portable. 此代码应相对可移植。 qgetenv("HOME") is Unix-specific. qgetenv("HOME")是特定于Unix的。 You should not introduce gratuituous platform-specific code in Qt-based projects -- it sort of defeats the purpose of using Qt in the first place. 您不应该在基于Qt的项目中引入特定于平台的免费代码-这样做有悖于首先使用Qt的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM