diff --git a/src/corelib/kernel/qcore_foundation.mm b/src/corelib/kernel/qcore_foundation.mm index 623d462749..eb3c70756a 100644 --- a/src/corelib/kernel/qcore_foundation.mm +++ b/src/corelib/kernel/qcore_foundation.mm @@ -197,7 +197,7 @@ NSData *QByteArray::toRawNSData() const Constructs a new QString containing a copy of the \a string CFString. - \note this function is only available on OS X and iOS. + \note this function is only available on \macos and iOS. */ QString QString::fromCFString(CFStringRef string) { @@ -222,11 +222,24 @@ QString QString::fromCFString(CFStringRef string) Creates a CFString from a QString. The caller owns the CFString and is responsible for releasing it. - \note this function is only available on OS X and iOS. + \note this function is only available on \macos and iOS. */ CFStringRef QString::toCFString() const { - return CFStringCreateWithCharacters(0, reinterpret_cast(unicode()), length()); + return QStringView{*this}.toCFString(); +} + +/*! + \since 6.0 + + Creates a CFString from this QStringView. The caller owns the CFString and is + responsible for releasing it. + + \note this function is only available on \macos and iOS. +*/ +CFStringRef QStringView::toCFString() const +{ + return CFStringCreateWithCharacters(0, reinterpret_cast(data()), size()); } /*! @@ -234,7 +247,7 @@ CFStringRef QString::toCFString() const Constructs a new QString containing a copy of the \a string NSString. - \note this function is only available on OS X and iOS. + \note this function is only available on \macos and iOS. */ QString QString::fromNSString(const NSString *string) { @@ -251,11 +264,23 @@ QString QString::fromNSString(const NSString *string) Creates a NSString from a QString. The NSString is autoreleased. - \note this function is only available on OS X and iOS. + \note this function is only available on \macos and iOS. */ NSString *QString::toNSString() const { - return [NSString stringWithCharacters: reinterpret_cast(unicode()) length: length()]; + return QStringView{*this}.toNSString(); +} + +/*! + \since 6.0 + + Creates an NSString from this QStringView. The NSString is autoreleased. + + \note this function is only available on \macos and iOS. +*/ +NSString *QStringView::toNSString() const +{ + return [NSString stringWithCharacters:reinterpret_cast(data()) length:size()]; } // ---------------------------------------------------------------------------- diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index a2e55f5b16..2dd933284b 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -51,6 +51,11 @@ #include +#if defined(Q_OS_DARWIN) || defined(Q_QDOC) +Q_FORWARD_DECLARE_CF_TYPE(CFString); +Q_FORWARD_DECLARE_OBJC_CLASS(NSString); +#endif + QT_BEGIN_NAMESPACE class QString; @@ -215,6 +220,11 @@ public: : QStringView(str.data(), qsizetype(str.size())) {} Q_REQUIRED_RESULT inline QString toString() const; // defined in qstring.h +#if defined(Q_OS_DARWIN) || defined(Q_QDOC) + // defined in qcore_foundation.mm + Q_REQUIRED_RESULT Q_CORE_EXPORT CFStringRef toCFString() const Q_DECL_CF_RETURNS_RETAINED; + Q_REQUIRED_RESULT Q_CORE_EXPORT NSString *toNSString() const Q_DECL_NS_RETURNS_AUTORELEASED; +#endif Q_REQUIRED_RESULT Q_DECL_CONSTEXPR qsizetype size() const noexcept { return m_size; } Q_REQUIRED_RESULT const_pointer data() const noexcept { return reinterpret_cast(m_data); } diff --git a/src/plugins/platforms/cocoa/qcocoamenuitem.mm b/src/plugins/platforms/cocoa/qcocoamenuitem.mm index c117ce6a84..653e4a337e 100644 --- a/src/plugins/platforms/cocoa/qcocoamenuitem.mm +++ b/src/plugins/platforms/cocoa/qcocoamenuitem.mm @@ -103,7 +103,7 @@ NSString *keySequenceToKeyEqivalent(const QKeySequence &accel) // Similar to qt_mac_removePrivateUnicode change the delete key so the symbol is correctly seen in native menubar if (cocoa_key.unicode() == NSDeleteFunctionKey) cocoa_key = NSDeleteCharacter; - return [NSString stringWithCharacters:&cocoa_key.unicode() length:1]; + return QStringView{&cocoa_key, 1}.toNSString(); } // return the cocoa modifier mask for the QKeySequence (currently only looks at the first one).