From d7d12bc1eb8ddb9b3f5055099fda7f6a2cea03b3 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 23 Apr 2014 14:56:18 +0200 Subject: [PATCH] Fix crash when accessing QFileDialog::selectedFiles() before widgets exist. Discovered while investigating: Task-number: QTBUG-38414 Change-Id: I764195254ba4b54c86079c6e8ef750d6249960d9 Reviewed-by: Shawn Rutledge --- src/widgets/dialogs/qfiledialog.cpp | 4 ++-- .../widgets/dialogs/qfiledialog/tst_qfiledialog.cpp | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index 80e8d152ff..00ff3a2eaf 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the QtWidgets module of the Qt Toolkit. @@ -1255,7 +1255,7 @@ QStringList QFileDialog::selectedFiles() const QStringList files; foreach (const QUrl &file, d->userSelectedFiles()) files.append(file.toLocalFile()); - if (files.isEmpty()) { + if (files.isEmpty() && d->usingWidgets()) { const FileMode fm = fileMode(); if (fm != ExistingFile && fm != ExistingFiles) files.append(d->rootIndex().data(QFileSystemModel::FilePathRole).toString()); diff --git a/tests/auto/widgets/dialogs/qfiledialog/tst_qfiledialog.cpp b/tests/auto/widgets/dialogs/qfiledialog/tst_qfiledialog.cpp index de8528216d..e856c2efc5 100644 --- a/tests/auto/widgets/dialogs/qfiledialog/tst_qfiledialog.cpp +++ b/tests/auto/widgets/dialogs/qfiledialog/tst_qfiledialog.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -147,6 +147,7 @@ private slots: void clearLineEdit(); void enableChooseButton(); void widgetlessNativeDialog(); + void selectedFilesWithoutWidgets(); void trailingDotsAndSpaces(); #ifdef Q_OS_UNIX #ifdef QT_BUILD_INTERNAL @@ -1326,6 +1327,14 @@ void tst_QFiledialog::widgetlessNativeDialog() QVERIFY(!button); } +void tst_QFiledialog::selectedFilesWithoutWidgets() +{ + // Test for a crash when widgets are not instantiated yet. + QFileDialog fd; + fd.setAcceptMode(QFileDialog::AcceptOpen); + QVERIFY(fd.selectedFiles().size() >= 0); +} + void tst_QFiledialog::trailingDotsAndSpaces() { #ifndef Q_OS_WIN