选择一个文件夹后QFileDialog

选择一个文件夹后QFileDialog

本文介绍了选择一个文件夹后QFileDialog :: getExistingDirectory没有关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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);

当我调试dlg-> close()返回true时,代码delete(dlg)被命中.文件夹选择器对话框仍然没有关闭.

When I debug the dlg->close() returns true and the code delete(dlg) is hit. Still the Folder chooser dialog box is not closing.

我正在使用Ubuntu 11.10 64位操作系统.使用存储库中的Qt库.

I am using Ubuntu 11.10 64 bit OS. Using Qt libraries from the repository.

我的最终目的只是显示一个文件夹选择器对话框,选择文件夹后,该对话框应关闭.之后,处理应继续.该怎么做?

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?

谢谢.

推荐答案

即使QFileDialog::getExistingDirectory是静态的并且不需要QFileDialog对象也可以工作,当最终选择目录时,它应该关闭对话框窗口.默认情况下,该功能会尝试打开本机文件对话框窗口,这似乎在某些平台上会引起一些问题.

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.By default that function tries to open a native file dialog window, which seems to cause some problems on some platforms.

您应该尝试通过添加选项DontUseNativeDialog来强制使用非本机对话框:

You should try forcing a non-native dialog by adding the option DontUseNativeDialog:

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

并删除其他两行(使用new QFileDialogif(dlg->close()) ...).

And remove the two other lines (with new QFileDialog and if(dlg->close()) ...).

这篇关于选择一个文件夹后QFileDialog :: getExistingDirectory没有关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:44