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
qqmlerror.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
4#include "qqmlerror.h"
5#include "qqmlfile.h"
6#include <private/qqmljsdiagnosticmessage_p.h>
7
8#include <QtCore/qdebug.h>
9#include <QtCore/qfile.h>
10#include <QtCore/qstringlist.h>
11#include <QtCore/qvector.h>
12
13#include <QtCore/qobject.h>
14#include <QtCore/qpointer.h>
15
17
44{
45public:
47 QPointer<QObject> object;
50 int line = -1;
51 int column = -1;
52
53 friend bool operator==(const QQmlErrorPrivate &a, const QQmlErrorPrivate &b)
54 {
55 return a.url == b.url && a.object == b.object && a.message == b.message
56 && a.type == b.type && a.line == b.line && a.column == b.column;
57 }
58};
59
67
72: d(nullptr)
73{
74 *this = other;
75}
76
81{
82 if (!other.d) {
83 delete d;
84 d = nullptr;
85 } else {
86 if (!d)
87 d = new QQmlErrorPrivate;
88 d->url = other.d->url;
89 d->message = other.d->message;
90 d->line = other.d->line;
91 d->column = other.d->column;
92 d->object = other.d->object;
93 d->type = other.d->type;
94 }
95 return *this;
96}
97
102{
103 delete d; d = nullptr;
104}
105
110{
111 return d != nullptr;
112}
113
118{
119 if (d)
120 return d->url;
121 return QUrl();
122}
123
128{
129 if (!d)
130 d = new QQmlErrorPrivate;
131 d->url = url;
132}
133
138{
139 if (d)
140 return d->message;
141 return QString();
142}
143
147void QQmlError::setDescription(const QString &description)
148{
149 if (!d)
150 d = new QQmlErrorPrivate;
151 d->message = description;
152}
153
158{
159 if (d)
160 return d->line;
161 return -1;
162}
163
168{
169 if (!d)
170 d = new QQmlErrorPrivate;
171 d->line = line;
172}
173
178{
179 if (d)
180 return d->column;
181 return -1;
182}
183
188{
189 if (!d)
190 d = new QQmlErrorPrivate;
191 d->column = column;
192}
193
201{
202 if (d)
203 return d->object;
204 return nullptr;
205}
206
211{
212 if (!d)
213 d = new QQmlErrorPrivate;
214 d->object = object;
215}
216
223{
224 if (d)
225 return d->type;
227}
228
236{
237 if (!d)
238 d = new QQmlErrorPrivate;
239 d->type = messageType;
240}
241
246{
247 QString rv;
248
249 QUrl u(url());
250 int l(line());
251
252 if (u.isEmpty() || (u.isLocalFile() && u.path().isEmpty()))
253 rv += QLatin1String("<Unknown File>");
254 else
255 rv += u.toString();
256
257 if (l != -1) {
258 rv += QLatin1Char(':') + QString::number(l);
259
260 int c(column());
261 if (c != -1)
262 rv += QLatin1Char(':') + QString::number(c);
263 }
264
265 rv += QLatin1String(": ") + description();
266
267 return rv;
268}
269
270bool operator==(const QQmlError &a, const QQmlError &b)
271{
272 return a.d == b.d || (a.d && b.d && *a.d == *b.d);
273}
274
283{
284 debug << qPrintable(error.toString());
285
286 QUrl url = error.url();
287
288 if (error.line() > 0 && (url.scheme() == QLatin1String("file") || url.scheme() == QLatin1String("qrc"))) {
290 QFile f(file);
291 if (f.open(QIODevice::ReadOnly)) {
292 QByteArray data = f.readAll();
294 const QString code = stream.readAll();
295 const auto lines = QStringView{code}.split(QLatin1Char('\n'));
296
297 if (lines.size() >= error.line()) {
298 const QStringView &line = lines.at(error.line() - 1);
299 debug << "\n " << line.toLocal8Bit().constData();
300
301 if(error.column() > 0) {
302 int column = qMax(0, error.column() - 1);
303 column = qMin(column, line.size());
304
305 QByteArray ind;
306 ind.reserve(column);
307 for (int i = 0; i < column; ++i) {
308 const QChar ch = line.at(i);
309 if (ch.isSpace())
310 ind.append(ch.unicode());
311 else
312 ind.append(' ');
313 }
314 ind.append('^');
315 debug << "\n " << ind.constData();
316 }
317 }
318 }
319 }
320 return debug;
321}
322
\inmodule QtCore
Definition qbytearray.h:57
void reserve(qsizetype size)
Attempts to allocate memory for at least size bytes.
Definition qbytearray.h:634
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
QByteArray & append(char c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qfile.h:93
\inmodule QtCore
Definition qobject.h:103
QPointer< QObject > object
Definition qqmlerror.cpp:47
friend bool operator==(const QQmlErrorPrivate &a, const QQmlErrorPrivate &b)
Definition qqmlerror.cpp:53
The QQmlError class encapsulates a QML error.
Definition qqmlerror.h:18
int column() const
Returns the error column number.
void setObject(QObject *)
Sets the nearest object where this error occurred.
QString description() const
Returns the error description.
QQmlError()
Creates an empty error object.
Definition qqmlerror.cpp:63
void setColumn(int)
Sets the error column number.
QObject * object() const
Returns the nearest object where this error occurred.
void setLine(int)
Sets the error line number.
QtMsgType messageType() const
int line() const
Returns the error line number.
QUrl url() const
Returns the url for the file that caused this error.
void setDescription(const QString &)
Sets the error description.
QQmlError & operator=(const QQmlError &)
Assigns other to this error object.
Definition qqmlerror.cpp:80
bool isValid() const
Returns true if this error is valid, otherwise false.
QString toString() const
Returns the error as a human readable string.
void setUrl(const QUrl &)
Sets the url for the file that caused this error.
void setMessageType(QtMsgType messageType)
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to \l{QFile}.
Definition qqmlfile.cpp:742
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QStringList split(const QString &sep, Qt::SplitBehavior behavior=Qt::KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Splits the string into substrings wherever sep occurs, and returns the list of those strings.
Definition qstring.cpp:8218
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
const QChar at(qsizetype i) const
Returns the character at the given index position in the string.
Definition qstring.h:1226
QByteArray toLocal8Bit() const &
Definition qstring.h:638
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
\inmodule QtCore
\inmodule QtCore
Definition qurl.h:94
bool isLocalFile() const
Definition qurl.cpp:3445
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition qurl.cpp:1896
QString scheme() const
Returns the scheme of the URL.
Definition qurl.cpp:1991
QString toString(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
Definition qurl.cpp:2831
QString path(ComponentFormattingOptions options=FullyDecoded) const
Returns the path of the URL.
Definition qurl.cpp:2468
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
EGLStreamKHR stream
QtMsgType
Definition qlogging.h:29
@ QtWarningMsg
Definition qlogging.h:31
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat f
GLenum type
GLenum GLenum GLsizei void GLsizei void * column
const GLubyte * c
bool operator==(const QQmlError &a, const QQmlError &b)
QDebug operator<<(QDebug debug, const QQmlError &error)
#define qPrintable(string)
Definition qstring.h:1531
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
QFile file
[0]
QUrl url("example.com")
[constructor-url-reference]
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
\inmodule QtCore \reentrant
Definition qchar.h:18