Skip to content

Commit

Permalink
Merge pull request #520 from overte-org/fix/script_crash
Browse files Browse the repository at this point in the history
Added null pointer check for asQuickItem()
  • Loading branch information
ksuprynowicz authored Aug 18, 2023
2 parents c6f9f16 + ae36233 commit 9c78c8f
Showing 1 changed file with 61 additions and 17 deletions.
78 changes: 61 additions & 17 deletions libraries/ui/src/QmlWindowClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,21 @@ void QmlWindowClass::qmlToScript(const QVariant& message) {

void QmlWindowClass::sendToQml(const QVariant& message) {
// Forward messages received from the script on to QML
QMetaObject::invokeMethod(asQuickItem(), "fromScript", Qt::QueuedConnection, Q_ARG(QVariant, message));
QQuickItem *quickItem = asQuickItem();
if (quickItem) {
QMetaObject::invokeMethod(quickItem, "fromScript", Qt::QueuedConnection, Q_ARG(QVariant, message));
} else {
qDebug() << "QmlWindowClass::sendToQml: asQuickItem() returned NULL";
}
}

void QmlWindowClass::clearDebugWindow() {
QMetaObject::invokeMethod(asQuickItem(), "clearDebugWindow", Qt::QueuedConnection);
QQuickItem *quickItem = asQuickItem();
if (quickItem) {
QMetaObject::invokeMethod(quickItem, "clearDebugWindow", Qt::QueuedConnection);
} else {
qDebug() << "QmlWindowClass::clearDebugWindow: asQuickItem() returned NULL";
}
}

void QmlWindowClass::emitScriptEvent(const QVariant& scriptMessage) {
Expand All @@ -192,9 +202,19 @@ void QmlWindowClass::emitWebEvent(const QVariant& webMessage) {
const QString LOWER_KEYBOARD = "_LOWER_KEYBOARD";
QString messageString = webMessage.type() == QVariant::String ? webMessage.toString() : "";
if (messageString.left(RAISE_KEYBOARD.length()) == RAISE_KEYBOARD) {
setKeyboardRaised(asQuickItem(), true, messageString == RAISE_KEYBOARD_NUMERIC);
QQuickItem *quickItem = asQuickItem();
if (quickItem) {
setKeyboardRaised(quickItem, true, messageString == RAISE_KEYBOARD_NUMERIC);
} else {
qDebug() << "QmlWindowClass::emitWebEvent: asQuickItem() returned NULL";
}
} else if (messageString == LOWER_KEYBOARD) {
setKeyboardRaised(asQuickItem(), false);
QQuickItem *quickItem = asQuickItem();
if (quickItem) {
setKeyboardRaised(quickItem, false);
} else {
qDebug() << "QmlWindowClass::emitWebEvent: asQuickItem() returned NULL";
}
} else {
emit webEventReceived(webMessage);
}
Expand Down Expand Up @@ -234,7 +254,12 @@ void QmlWindowClass::setVisible(bool visible) {
}

QQuickItem* targetWindow = asQuickItem();
targetWindow->setProperty(OFFSCREEN_VISIBILITY_PROPERTY, visible);
//TODO: all asQuickItem() need to be guarded like this
if (targetWindow) {
targetWindow->setProperty(OFFSCREEN_VISIBILITY_PROPERTY, visible);
} else {
qDebug() << "QmlWindowClass::setVisible: asQuickItem() returned NULL";
}
}

bool QmlWindowClass::isVisible() {
Expand All @@ -249,7 +274,12 @@ bool QmlWindowClass::isVisible() {
return false;
}

return asQuickItem()->isVisible();
QQuickItem *quickItem = asQuickItem();
if (quickItem) {
return quickItem->isVisible();
} else {
qDebug() << "QmlWindowClass::isVisible: asQuickItem() returned NULL";
}
}

glm::vec2 QmlWindowClass::getPosition() {
Expand All @@ -263,18 +293,24 @@ glm::vec2 QmlWindowClass::getPosition() {
return {};
}

return toGlm(asQuickItem()->position());
QQuickItem *quickItem = asQuickItem();
if (quickItem) {
return toGlm(quickItem->position());
} else {
qDebug() << "QmlWindowClass::getPosition: asQuickItem() returned NULL";
return glm::vec2(0.0f, 0.0f);
}
}


void QmlWindowClass::setPosition(const glm::vec2& position) {
if (QThread::currentThread() != thread()) {
QMetaObject::invokeMethod(this, "setPosition", Q_ARG(const glm::vec2&, position));
return;
}

if (!_qmlWindow.isNull()) {
asQuickItem()->setPosition(QPointF(position.x, position.y));
QQuickItem *quickItem = asQuickItem();
if (quickItem) {
quickItem->setPosition(QPointF(position.x, position.y));
}
}

Expand All @@ -298,7 +334,12 @@ glm::vec2 QmlWindowClass::getSize() {
return {};
}
QQuickItem* targetWindow = asQuickItem();
return vec2(targetWindow->width(), targetWindow->height());
if (targetWindow) {
return vec2(targetWindow->width(), targetWindow->height());
} else {
qDebug() << "QmlWindowClass::getSize: asQuickItem() returned NULL";
return vec2(0.0f, 0.0f);
}
}

void QmlWindowClass::setSize(const glm::vec2& size) {
Expand All @@ -307,8 +348,9 @@ void QmlWindowClass::setSize(const glm::vec2& size) {
return;
}

if (!_qmlWindow.isNull()) {
asQuickItem()->setSize(QSizeF(size.x, size.y));
QQuickItem *quickItem = asQuickItem();
if (quickItem) {
quickItem->setSize(QSizeF(size.x, size.y));
}
}

Expand All @@ -322,8 +364,9 @@ void QmlWindowClass::setTitle(const QString& title) {
return;
}

if (!_qmlWindow.isNull()) {
asQuickItem()->setProperty(TITLE_PROPERTY, title);
QQuickItem *quickItem = asQuickItem();
if (quickItem) {
quickItem->setProperty(TITLE_PROPERTY, title);
}
}

Expand Down Expand Up @@ -353,7 +396,8 @@ void QmlWindowClass::raise() {
return;
}

if (_qmlWindow) {
QMetaObject::invokeMethod(asQuickItem(), "raise", Qt::DirectConnection);
QQuickItem *quickItem = asQuickItem();
if (quickItem) {
QMetaObject::invokeMethod(quickItem, "raise", Qt::DirectConnection);
}
}

0 comments on commit 9c78c8f

Please sign in to comment.