diff --git a/cmake/QtInternalTargets.cmake b/cmake/QtInternalTargets.cmake index 21bd645639a..dc45b57eecd 100644 --- a/cmake/QtInternalTargets.cmake +++ b/cmake/QtInternalTargets.cmake @@ -152,6 +152,7 @@ target_link_libraries(PlatformToolInternal INTERFACE PlatformAppInternal) qt_internal_add_global_definition(QT_NO_JAVA_STYLE_ITERATORS) qt_internal_add_global_definition(QT_NO_NARROWING_CONVERSIONS_IN_CONNECT) +qt_internal_add_global_definition(QT_EXPLICIT_QFILE_CONSTRUCTION_FROM_PATH) if(WARNINGS_ARE_ERRORS) qt_internal_set_warnings_are_errors_flags(PlatformModuleInternal INTERFACE) diff --git a/src/corelib/io/qfile.cpp b/src/corelib/io/qfile.cpp index b1115ad15c0..bb4a4640174 100644 --- a/src/corelib/io/qfile.cpp +++ b/src/corelib/io/qfile.cpp @@ -226,6 +226,15 @@ QFile::QFile(QObject *parent) } /*! Constructs a new file object to represent the file with the given \a name. + +//! [qfile-explicit-constructor-note] + \note In versions up to and including Qt 6.8, this constructor is + implicit, for backward compatibility. Starting from Qt 6.9 this + constructor is unconditionally \c{explicit}. Users can force this + constructor to be \c{explicit} even in earlier versions of Qt by + defining the \c{QT_EXPLICIT_QFILE_CONSTRUCTION_FROM_PATH} macro + before including any Qt header. +//! [qfile-explicit-constructor-note] */ QFile::QFile(const QString &name) : QFileDevice(*new QFilePrivate, nullptr) @@ -1145,6 +1154,8 @@ qint64 QFile::size() const \since 6.0 Constructs a new file object to represent the file with the given \a name. + + \include qfile.cpp qfile-explicit-constructor-note */ /*! \fn QFile::QFile(const std::filesystem::path &name, QObject *parent) diff --git a/src/corelib/io/qfile.h b/src/corelib/io/qfile.h index 25f63d01c84..3ac6795495c 100644 --- a/src/corelib/io/qfile.h +++ b/src/corelib/io/qfile.h @@ -54,6 +54,13 @@ using ForceFilesystemPath = typename std::enable_if_t= QT_VERSION_CHECK(6, 9, 0)) || defined(QT_EXPLICIT_QFILE_CONSTRUCTION_FROM_PATH) +# define QFILE_MAYBE_EXPLICIT explicit +#else +# define QFILE_MAYBE_EXPLICIT Q_IMPLICIT +#endif + class Q_CORE_EXPORT QFile : public QFileDevice { #ifndef QT_NO_QOBJECT @@ -63,12 +70,12 @@ class Q_CORE_EXPORT QFile : public QFileDevice public: QFile(); - QFile(const QString &name); + QFILE_MAYBE_EXPLICIT QFile(const QString &name); #ifdef Q_QDOC - QFile(const std::filesystem::path &name); + QFILE_MAYBE_EXPLICIT QFile(const std::filesystem::path &name); #elif QT_CONFIG(cxx17_filesystem) template = 0> - QFile(const T &name) : QFile(QtPrivate::fromFilesystemPath(name)) + QFILE_MAYBE_EXPLICIT QFile(const T &name) : QFile(QtPrivate::fromFilesystemPath(name)) { } #endif // QT_CONFIG(cxx17_filesystem)