Logo Search packages:      
Sourcecode: qt4-x11 version File versions

QString QFileDialog::getOpenFileName ( QWidget parent = 0,
const QString caption = QString(),
const QString dir = QString(),
const QString filter = QString(),
QString selectedFilter = 0,
Options  options = 0 
) [static]

This is a convenience static function that returns an existing file selected by the user. If the user presses Cancel, it returns a null string.

doc/src/snippets/code/src_gui_dialogs_qfiledialog.cpp 8

The function creates a modal file dialog with the given parent widget. If the parent is not 0, the dialog will be shown centered over the parent widget.

The file dialog's working directory will be set to dir. If dir includes a file name, the file will be selected. Only files that match the given filter are shown. The filter selected is set to selectedFilter. The parameters dir, selectedFilter, and filter may be empty strings. The options argument holds various options about how to run the dialog, see the QFileDialog::Option enum for more information on the flags you can pass.

The dialog's caption is set to caption. If caption is not specified then a default caption will be used.

Under Windows and Mac OS X, this static function will use the native file dialog and not a QFileDialog.

Note that on Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if parent is not 0 then it will position the dialog just under the parent's title bar.

Under Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if {/usr/tmp} is a symlink to {/var/tmp}, the file dialog will change to {/var/tmp} after entering {/usr/tmp}. If options includes DontResolveSymlinks, the file dialog will treat symlinks as regular directories.

See also:
getOpenFileNames(), getSaveFileName(), getExistingDirectory()

Definition at line 1322 of file qfiledialog.cpp.

References QDialog::exec(), QList< T >::first(), QList< T >::isEmpty(), QString::isEmpty(), QString::normalized(), selectedFiles(), selectNameFilter(), and QList< T >::value().

Referenced by QWebPage::chooseFile(), ImageComposer::chooseImage(), MainWindow::chooseImage(), MainWindow::loadAct(), Dialog::loadFromFile(), AddressBook::loadFromFile(), ImageViewer::open(), and MainWindow::openFile().

{
    if (qt_filedialog_open_filename_hook)
        return qt_filedialog_open_filename_hook(parent, caption, dir, filter, selectedFilter, options);
    QFileDialogArgs args;
    args.parent = parent;
    args.caption = caption;
    args.directory = QFileDialogPrivate::workingDirectory(dir);
    args.selection = QFileDialogPrivate::initialSelection(dir);
    args.filter = filter;
    args.mode = ExistingFile;
    args.options = options;
#if defined(Q_WS_WIN)
    if (qt_use_native_dialogs && !(args.options & DontUseNativeDialog)) {
        return qt_win_get_open_file_name(args, &(args.directory), selectedFilter);
    }
#elif defined(Q_WS_MAC)
    if (qt_use_native_dialogs && !(args.options & DontUseNativeDialog)) {
        if (dir.isEmpty())
            args.directory = dir;
        QStringList files = qt_mac_get_open_file_names(args, &(args.directory), selectedFilter);
        if (!files.isEmpty())
            return files.first().normalized(QString::NormalizationForm_C);
        return QString();
    }
#endif

    // create a qt dialog
    QFileDialog dialog(args);
    if (selectedFilter)
        dialog.selectNameFilter(*selectedFilter);
    if (dialog.exec() == QDialog::Accepted) {
        if (selectedFilter)
            *selectedFilter = dialog.selectedFilter();
        return dialog.selectedFiles().value(0);
    }
    return QString();
}


Generated by  Doxygen 1.6.0   Back to index