Qt 4.8
Public Functions | Properties | Related Functions | List of all members
QDeclarativeError Class Reference

The QDeclarativeError class encapsulates a QML error. More...

#include <qdeclarativeerror.h>

Public Functions

int column () const
 Returns the error column number. More...
 
QString description () const
 Returns the error description. More...
 
bool isValid () const
 Returns true if this error is valid, otherwise false. More...
 
int line () const
 Returns the error line number. More...
 
QDeclarativeErroroperator= (const QDeclarativeError &)
 Assigns other to this error object. More...
 
 QDeclarativeError ()
 Creates an empty error object. More...
 
 QDeclarativeError (const QDeclarativeError &)
 Creates a copy of other. More...
 
void setColumn (int)
 Sets the error column number. More...
 
void setDescription (const QString &)
 Sets the error description. More...
 
void setLine (int)
 Sets the error line number. More...
 
void setUrl (const QUrl &)
 Sets the url for the file that caused this error. More...
 
QString toString () const
 Returns the error as a human readable string. More...
 
QUrl url () const
 Returns the url for the file that caused this error. More...
 
 ~QDeclarativeError ()
 

Properties

QDeclarativeErrorPrivated
 

Related Functions

(Note that these are not member functions.)

QDebug operator<< (QDebug debug, const QDeclarativeError &error)
 Outputs a human readable version of error to debug. More...
 

Detailed Description

The QDeclarativeError class encapsulates a QML error.

Since
4.7

QDeclarativeError includes a textual description of the error, as well as location information (the file, line, and column). The toString() method creates a single-line, human-readable string containing all of this information, for example:

file:///home/user/test.qml:7:8: Invalid property assignment: double expected

You can use qDebug() or qWarning() to output errors to the console. This method will attempt to open the file indicated by the error and include additional contextual information.

file:
y: "hello"
^
See also
QDeclarativeView::errors(), QDeclarativeComponent::errors()

Definition at line 56 of file qdeclarativeerror.h.

Constructors and Destructors

◆ QDeclarativeError() [1/2]

QDeclarativeError::QDeclarativeError ( )

Creates an empty error object.

Definition at line 96 of file qdeclarativeerror.cpp.

97 : d(0)
98 {
99 }
QDeclarativeErrorPrivate * d

◆ QDeclarativeError() [2/2]

QDeclarativeError::QDeclarativeError ( const QDeclarativeError other)

Creates a copy of other.

Definition at line 104 of file qdeclarativeerror.cpp.

105 : d(0)
106 {
107  *this = other;
108 }
QDeclarativeErrorPrivate * d

◆ ~QDeclarativeError()

QDeclarativeError::~QDeclarativeError ( )
Warning
This function is not part of the public interface.

Definition at line 131 of file qdeclarativeerror.cpp.

132 {
133  delete d; d = 0;
134 }
QDeclarativeErrorPrivate * d

Functions

◆ column()

int QDeclarativeError::column ( ) const

Returns the error column number.

Definition at line 201 of file qdeclarativeerror.cpp.

Referenced by QDeclarativeEnginePrivate::createQmlObject(), operator<<(), setColumn(), and toString().

202 {
203  if (d) return d->column;
204  else return -1;
205 }
QDeclarativeErrorPrivate * d

◆ description()

QString QDeclarativeError::description ( ) const

Returns the error description.

Definition at line 165 of file qdeclarativeerror.cpp.

Referenced by QDeclarativeCompiler::compile(), QDeclarativeEnginePrivate::createQmlObject(), QDeclarativeDirParser::errors(), QDeclarativeComponent::errorString(), QDeclarativeImportsPrivate::importExtension(), setDescription(), and toString().

166 {
167  if (d) return d->description;
168  else return QString();
169 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
QDeclarativeErrorPrivate * d

◆ isValid()

bool QDeclarativeError::isValid ( ) const

Returns true if this error is valid, otherwise false.

Definition at line 139 of file qdeclarativeerror.cpp.

140 {
141  return d != 0;
142 }
QDeclarativeErrorPrivate * d

◆ line()

int QDeclarativeError::line ( ) const

Returns the error line number.

Definition at line 183 of file qdeclarativeerror.cpp.

Referenced by QDeclarativeEnginePrivate::createQmlObject(), QDeclarativeComponent::errorString(), operator<<(), setLine(), and toString().

184 {
185  if (d) return d->line;
186  else return -1;
187 }
QDeclarativeErrorPrivate * d

◆ operator=()

QDeclarativeError & QDeclarativeError::operator= ( const QDeclarativeError other)

Assigns other to this error object.

Definition at line 113 of file qdeclarativeerror.cpp.

114 {
115  if (!other.d) {
116  delete d;
117  d = 0;
118  } else {
119  if (!d) d = new QDeclarativeErrorPrivate;
120  d->url = other.d->url;
121  d->description = other.d->description;
122  d->line = other.d->line;
123  d->column = other.d->column;
124  }
125  return *this;
126 }
QDeclarativeErrorPrivate * d

◆ setColumn()

void QDeclarativeError::setColumn ( int  column)

◆ setDescription()

void QDeclarativeError::setDescription ( const QString description)

◆ setLine()

void QDeclarativeError::setLine ( int  line)

◆ setUrl()

void QDeclarativeError::setUrl ( const QUrl url)

◆ toString()

QString QDeclarativeError::toString ( ) const

Returns the error as a human readable string.

Definition at line 219 of file qdeclarativeerror.cpp.

Referenced by QDeclarativeEnginePrivate::createQmlObject(), dumpwarning(), and operator<<().

220 {
221  QString rv;
222  if (url().isEmpty()) {
223  rv = QLatin1String("<Unknown File>");
224  } else if (line() != -1) {
225  rv = url().toString() + QLatin1Char(':') + QString::number(line());
226  if(column() != -1)
227  rv += QLatin1Char(':') + QString::number(column());
228  } else {
229  rv = url().toString();
230  }
231 
232  rv += QLatin1String(": ") + description();
233 
234  return rv;
235 }
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:6448
QUrl url() const
Returns the url for the file that caused this error.
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
static bool isEmpty(const char *str)
QString description() const
Returns the error description.
int line() const
Returns the error line number.
int column() const
Returns the error column number.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ url()

QUrl QDeclarativeError::url ( ) const

Returns the url for the file that caused this error.

Definition at line 147 of file qdeclarativeerror.cpp.

Referenced by QDeclarativeEnginePrivate::createQmlObject(), QDeclarativeComponent::errorString(), operator<<(), setUrl(), and toString().

148 {
149  if (d) return d->url;
150  else return QUrl();
151 }
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
QDeclarativeErrorPrivate * d

Friends and Related Functions

◆ operator<<()

QDebug operator<< ( QDebug  debug,
const QDeclarativeError error 
)
related

Outputs a human readable version of error to debug.

Definition at line 247 of file qdeclarativeerror.cpp.

248 {
249  debug << qPrintable(error.toString());
250 
251  QUrl url = error.url();
252 
253  if (error.line() > 0 && url.scheme() == QLatin1String("file")) {
254  QString file = url.toLocalFile();
255  QFile f(file);
256  if (f.open(QIODevice::ReadOnly)) {
257  QByteArray data = f.readAll();
259 #ifndef QT_NO_TEXTCODEC
260  stream.setCodec("UTF-8");
261 #endif
262  const QString code = stream.readAll();
263  const QStringList lines = code.split(QLatin1Char('\n'));
264 
265  if (lines.count() >= error.line()) {
266  const QString &line = lines.at(error.line() - 1);
267  debug << "\n " << qPrintable(line);
268 
269  if(error.column() > 0) {
270  int column = qMax(0, error.column() - 1);
271  column = qMin(column, line.length());
272 
273  QByteArray ind;
274  ind.reserve(column);
275  for (int i = 0; i < column; ++i) {
276  const QChar ch = line.at(i);
277  if (ch.isSpace())
278  ind.append(ch.unicode());
279  else
280  ind.append(' ');
281  }
282  ind.append('^');
283  debug << "\n " << ind.constData();
284  }
285  }
286  }
287  }
288  return debug;
289 }
QUrl url() const
Returns the url for the file that caused this error.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
bool isSpace() const
Returns true if the character is a separator character (Separator_* categories); otherwise returns fa...
Definition: qchar.cpp:609
QString toString() const
Returns the error as a human readable string.
static FILE * stream
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static const char * data(const QByteArray &arr)
int line() const
Returns the error line number.
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:73
int column() const
Returns the error column number.
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
void reserve(int size)
Attempts to allocate memory for at least size bytes.
Definition: qbytearray.h:449
#define qPrintable(string)
Definition: qglobal.h:1750
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

Properties

◆ d

QDeclarativeErrorPrivate* QDeclarativeError::d
private

The documentation for this class was generated from the following files: