Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qqmlstringconverters_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQMLSTRINGCONVERTERS_P_H
5#define QQMLSTRINGCONVERTERS_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qglobal.h>
19#include <QtCore/qvariant.h>
20
21#include <private/qtqmlglobal_p.h>
22
24
25class QPointF;
26class QSizeF;
27class QRectF;
28class QString;
29class QByteArray;
30
32{
33 Q_QML_EXPORT QVariant variantFromString(const QString &, QMetaType preferredType, bool *ok = nullptr);
34
35 Q_QML_EXPORT QVariant colorFromString(const QString &, bool *ok = nullptr);
36 Q_QML_EXPORT unsigned rgbaFromString(const QString &, bool *ok = nullptr);
37
38#if QT_CONFIG(datestring)
39 Q_QML_EXPORT QDate dateFromString(const QString &, bool *ok = nullptr);
40 Q_QML_EXPORT QTime timeFromString(const QString &, bool *ok = nullptr);
41 Q_QML_EXPORT QDateTime dateTimeFromString(const QString &, bool *ok = nullptr);
42#endif
43 Q_QML_EXPORT QPointF pointFFromString(const QString &, bool *ok = nullptr);
44 Q_QML_EXPORT QSizeF sizeFFromString(const QString &, bool *ok = nullptr);
45 Q_QML_EXPORT QRectF rectFFromString(const QString &, bool *ok = nullptr);
46
47 // checks if the string contains a list of doubles separated by separators, like "double1
48 // separators1 double2 separators2 ..." for example.
49 template<int NumParams, char16_t... separators>
50 bool isValidNumberString(const QString &s, std::array<double, NumParams> *numbers = nullptr)
51 {
53 NumParams == 2 || NumParams == 3 || NumParams == 4 || NumParams == 16,
54 "Unsupported number of params; add an additional case below if necessary.");
55 constexpr std::array<char16_t, NumParams - 1> separatorArray{ separators... };
56 // complain about missing separators when first or last entry is initialized with 0
57 Q_STATIC_ASSERT_X(separatorArray[0] != 0,
58 "Did not specify any separators for isValidNumberString.");
59 Q_STATIC_ASSERT_X(separatorArray[NumParams - 2] != 0,
60 "Did not specify enough separators for isValidNumberString.");
61
62 bool floatOk = true;
64 for (qsizetype i = 0; i < NumParams - 1; ++i) {
65 const qsizetype commaIndex = view.indexOf(separatorArray[i]);
66 if (commaIndex == -1)
67 return false;
68 const auto current = view.first(commaIndex).toDouble(&floatOk);
69 if (!floatOk)
70 return false;
71 if (numbers)
72 (*numbers)[i] = current;
73
74 view = view.sliced(commaIndex + 1);
75 }
76 const auto current = view.toDouble(&floatOk);
77 if (!floatOk)
78 return false;
79 if (numbers)
80 (*numbers)[NumParams - 1] = current;
81
82 return true;
83 }
84
85 // Constructs a value type T from the given string that contains NumParams double values
86 // separated by separators, like "double1 separators1 double2 separators2 ..." for example.
87 template<typename T, int NumParams, char16_t... separators>
88 T valueTypeFromNumberString(const QString &s, bool *ok = nullptr)
89 {
91 NumParams == 2 || NumParams == 3 || NumParams == 4 || NumParams == 16,
92 "Unsupported number of params; add an additional case below if necessary.");
93
94 std::array<double, NumParams> parameters;
95 if (!isValidNumberString<NumParams, separators...>(s, &parameters)) {
96 if (ok)
97 *ok = false;
98 return T{};
99 }
100
101 if (ok)
102 *ok = true;
103
104 if constexpr (NumParams == 2) {
105 return T(parameters[0], parameters[1]);
106 } else if constexpr (NumParams == 3) {
107 return T(parameters[0], parameters[1], parameters[2]);
108 } else if constexpr (NumParams == 4) {
109 return T(parameters[0], parameters[1], parameters[2], parameters[3]);
110 } else if constexpr (NumParams == 16) {
111 return T(parameters[0], parameters[1], parameters[2], parameters[3], parameters[4],
112 parameters[5], parameters[6], parameters[7], parameters[8], parameters[9],
113 parameters[10], parameters[11], parameters[12], parameters[13], parameters[14],
114 parameters[15]);
115 }
116
117 Q_UNREACHABLE_RETURN(T{});
118 }
119}
120
122
123#endif // QQMLSTRINGCONVERTERS_P_H
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore\reentrant
Definition qdatetime.h:283
\inmodule QtCore \reentrant
Definition qdatetime.h:29
\inmodule QtCore
Definition qmetatype.h:341
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore
Definition qsize.h:208
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore \reentrant
Definition qdatetime.h:215
\inmodule QtCore
Definition qvariant.h:65
Q_QML_EXPORT QPointF pointFFromString(const QString &, bool *ok=nullptr)
T valueTypeFromNumberString(const QString &s, bool *ok=nullptr)
Q_QML_EXPORT unsigned rgbaFromString(const QString &, bool *ok=nullptr)
Q_QML_EXPORT QSizeF sizeFFromString(const QString &, bool *ok=nullptr)
bool isValidNumberString(const QString &s, std::array< double, NumParams > *numbers=nullptr)
Q_QML_EXPORT QVariant variantFromString(const QString &, QMetaType preferredType, bool *ok=nullptr)
Q_QML_EXPORT QRectF rectFFromString(const QString &, bool *ok=nullptr)
Q_QML_EXPORT QVariant colorFromString(const QString &, bool *ok=nullptr)
Combined button and popup list for selecting options.
#define Q_STATIC_ASSERT_X(Condition, Message)
Definition qassert.h:111
GLdouble s
[6]
Definition qopenglext.h:235
static std::optional< QDate > dateFromString(const QString &string, QV4::ExecutionEngine *engine)
\qmlmethod string Qt::formatDate(datetime date, variant format, variant localeFormatOption)
static std::optional< QTime > timeFromString(const QString &string, QV4::ExecutionEngine *engine)
\qmlmethod string Qt::formatTime(datetime time, variant format, variant localeFormatOption)
static std::optional< QDateTime > dateTimeFromString(const QString &string, QV4::ExecutionEngine *engine)
\qmlmethod string Qt::formatDateTime(datetime dateTime, variant format, variant localeFormatOption)
ptrdiff_t qsizetype
Definition qtypes.h:165
QQuickView * view
[0]