本页内容

QQmlError Class

QQmlError 类封装了一个 QML 错误。更多内容...

头文件: #include <QQmlError>
CMake: find_package(Qt6 REQUIRED COMPONENTS Qml)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
qmake: QT += qml

公共函数

QQmlError()
QQmlError(const QQmlError &other)
int column() const
QString description() const
bool isValid() const
int line() const
QtMsgType messageType() const
QObject *object() const
void setColumn(int column)
void setDescription(const QString &description)
void setLine(int line)
void setMessageType(QtMsgType messageType)
void setObject(QObject *object)
void setUrl(const QUrl &url)
QString toString() const
QUrl url() const
QQmlError &operator=(const QQmlError &other)
QDebug operator<<(QDebug debug, const QQmlError &error)

详细描述

QQmlError 包含错误的文本描述以及位置信息(文件、行号和列号)。toString() 方法会生成一个单行、易于人类阅读的字符串,其中包含所有这些信息,例如:

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

您可以使用qDebug()、qInfo() 或qWarning() 将错误输出到控制台。该方法将尝试打开错误所指的文件,并包含额外的上下文信息。

file:///home/user/test.qml:7:8: Invalid property assignment: double expected
        y: "hello"
           ^

另请参阅 QQuickView::errors() 和QQmlComponent::errors()。

成员函数文档

QQmlError::QQmlError()

创建一个空的错误对象。

QQmlError::QQmlError(const QQmlError &other)

创建other 的副本。

int QQmlError::column() const

返回错误列的编号。

另请参阅 setColumn()。

QString QQmlError::description() const

返回错误描述。

另请参阅 setDescription()。

bool QQmlError::isValid() const

如果该错误有效,则返回 true;否则返回 false。

int QQmlError::line() const

返回错误行的行号。

另请参阅 setLine()。

QtMsgType QQmlError::messageType() const

返回消息类型。

另请参阅 setMessageType()。

QObject *QQmlError::object() const

返回发生此错误的最近对象。在绑定属性表达式中发生的异常会将此值设置为该属性所属的对象。对于所有其他异常,此值将为 0。

另请参阅 setObject()。

void QQmlError::setColumn(int column)

设置column 的错误编号。

另请参阅 column()。

void QQmlError::setDescription(const QString &description)

设置错误description 。

另请参阅 description()。

void QQmlError::setLine(int line)

设置line 的错误编号。

另请参阅 line()。

void QQmlError::setMessageType(QtMsgType messageType)

设置此消息的messageType 。消息类型决定了哪些QDebug 处理程序负责接收该消息。

另请参阅 messageType()。

void QQmlError::setObject(QObject *object)

设置发生此错误的最近object 。

另请参阅 object()。

void QQmlError::setUrl(const QUrl &url)

设置引发此错误的文件的url 。

另请参阅 url()。

QString QQmlError::toString() const

将错误作为易于理解的字符串返回。

QUrl QQmlError::url() const

返回导致此错误的文件的 URL。

另请参阅 setUrl()。

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

将other 赋值给此错误对象。

相关的非成员

QDebug operator<<(QDebug debug, const QQmlError &error)

将error 的可读版本输出到debug 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.