Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmlstringconverters.cpp
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
5#include <private/qqmlglobal_p.h>
6
7#include <QtCore/qpoint.h>
8#include <QtCore/qrect.h>
9#include <QtCore/qsize.h>
10#include <QtCore/qvariant.h>
11#include <QtCore/qdatetime.h>
12
14
16{
17 switch (preferredType.id()) {
18 case QMetaType::Int:
19 return QVariant(int(qRound(s.toDouble(ok))));
20 case QMetaType::UInt:
21 return QVariant(uint(qRound(s.toDouble(ok))));
22#if QT_CONFIG(datestring)
23 case QMetaType::QDate:
25 case QMetaType::QTime:
27 case QMetaType::QDateTime:
29#endif // datestring
30 case QMetaType::QPointF:
32 case QMetaType::QPoint:
33 return QVariant::fromValue(pointFFromString(s, ok).toPoint());
34 case QMetaType::QSizeF:
36 case QMetaType::QSize:
37 return QVariant::fromValue(sizeFFromString(s, ok).toSize());
38 case QMetaType::QRectF:
40 case QMetaType::QRect:
41 return QVariant::fromValue(rectFFromString(s, ok).toRect());
42 default: {
44 if (ret.isValid()) {
45 if (ok)
46 *ok = true;
47 return ret;
48 }
49 if (ok)
50 *ok = false;
51 return QVariant();
52 }
53 }
54}
55
57{
59}
60
62{
64}
65
66#if QT_CONFIG(datestring)
67QDate QQmlStringConverters::dateFromString(const QString &s, bool *ok)
68{
69 QDate d = QDate::fromString(s, Qt::ISODate);
70 if (ok) *ok = d.isValid();
71 return d;
72}
73
74QTime QQmlStringConverters::timeFromString(const QString &s, bool *ok)
75{
76 QTime t = QTime::fromString(s, Qt::ISODate);
77 if (ok) *ok = t.isValid();
78 return t;
79}
80
81QDateTime QQmlStringConverters::dateTimeFromString(const QString &s, bool *ok)
82{
83 QDateTime d = QDateTime::fromString(s, Qt::ISODate);
84 if (ok) *ok = d.isValid();
85 return d;
86}
87#endif // datestring
88
89//expects input of "x,y"
91{
92 if (s.count(QLatin1Char(',')) != 1) {
93 if (ok)
94 *ok = false;
95 return QPointF();
96 }
97
98 bool xGood, yGood;
99 int index = s.indexOf(QLatin1Char(','));
100 qreal xCoord = QStringView{s}.left(index).toDouble(&xGood);
101 qreal yCoord = QStringView{s}.mid(index+1).toDouble(&yGood);
102 if (!xGood || !yGood) {
103 if (ok)
104 *ok = false;
105 return QPointF();
106 }
107
108 if (ok)
109 *ok = true;
110 return QPointF(xCoord, yCoord);
111}
112
113//expects input of "widthxheight"
115{
116 if (s.count(QLatin1Char('x')) != 1) {
117 if (ok)
118 *ok = false;
119 return QSizeF();
120 }
121
122 bool wGood, hGood;
123 int index = s.indexOf(QLatin1Char('x'));
125 qreal height = QStringView{s}.mid(index+1).toDouble(&hGood);
126 if (!wGood || !hGood) {
127 if (ok)
128 *ok = false;
129 return QSizeF();
130 }
131
132 if (ok)
133 *ok = true;
134 return QSizeF(width, height);
135}
136
137//expects input of "x,y,widthxheight" //### use space instead of second comma?
139{
140 if (s.count(QLatin1Char(',')) != 2 || s.count(QLatin1Char('x')) != 1) {
141 if (ok)
142 *ok = false;
143 return QRectF();
144 }
145
146 bool xGood, yGood, wGood, hGood;
147 int index = s.indexOf(QLatin1Char(','));
148 qreal x = QStringView{s}.left(index).toDouble(&xGood);
149 int index2 = s.indexOf(QLatin1Char(','), index+1);
150 qreal y = QStringView{s}.mid(index+1, index2-index-1).toDouble(&yGood);
151 index = s.indexOf(QLatin1Char('x'), index2+1);
152 qreal width = QStringView{s}.mid(index2+1, index-index2-1).toDouble(&wGood);
153 qreal height = QStringView{s}.mid(index+1).toDouble(&hGood);
154 if (!xGood || !yGood || !wGood || !hGood) {
155 if (ok)
156 *ok = false;
157 return QRectF();
158 }
159
160 if (ok)
161 *ok = true;
162 return QRectF(x, y, width, height);
163}
164
\inmodule QtCore\reentrant
Definition qdatetime.h:257
\inmodule QtCore \reentrant
Definition qdatetime.h:27
\inmodule QtCore
Definition qmetatype.h:320
int id(int=0) const
Definition qmetatype.h:454
\inmodule QtCore\reentrant
Definition qpoint.h:214
virtual QVariant colorFromString(const QString &, bool *)
virtual unsigned rgbaFromString(const QString &, bool *)
static QVariant createValueType(const QJSValue &, QMetaType)
\inmodule QtCore\reentrant
Definition qrect.h:483
\inmodule QtCore
Definition qsize.h:207
\inmodule QtCore
Definition qstringview.h:76
Q_CORE_EXPORT double toDouble(bool *ok=nullptr) const
Returns the string view converted to a double value.
Definition qstring.cpp:7647
constexpr QStringView left(qsizetype n) const noexcept
constexpr QStringView mid(qsizetype pos, qsizetype n=-1) const noexcept
Returns the substring of length length starting at position start in this object.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore \reentrant
Definition qdatetime.h:189
\inmodule QtCore
Definition qvariant.h:64
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:531
Q_QML_PRIVATE_EXPORT QPointF pointFFromString(const QString &, bool *ok=nullptr)
Q_QML_PRIVATE_EXPORT unsigned rgbaFromString(const QString &, bool *ok=nullptr)
Q_QML_PRIVATE_EXPORT QSizeF sizeFFromString(const QString &, bool *ok=nullptr)
Q_QML_PRIVATE_EXPORT QVariant variantFromString(const QString &, QMetaType preferredType, bool *ok=nullptr)
Q_QML_PRIVATE_EXPORT QRectF rectFFromString(const QString &, bool *ok=nullptr)
Q_QML_PRIVATE_EXPORT QVariant colorFromString(const QString &, bool *ok=nullptr)
Combined button and popup list for selecting options.
@ ISODate
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:281
return ret
GLint GLint GLint GLint GLint x
[0]
GLint GLsizei GLsizei height
GLuint index
[2]
GLint GLsizei width
GLint y
GLdouble GLdouble t
Definition qopenglext.h:243
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)
Q_AUTOTEST_EXPORT QQmlColorProvider * QQml_colorProvider(void)
unsigned int uint
Definition qtypes.h:29
double qreal
Definition qtypes.h:92
\inmodule QtCore \reentrant
Definition qchar.h:17