2

I run script file with QQmlEngine.

I passing QVector3D value to qml (QQmlEngine):

QVector3D dir(1,2,3);
QQmlEngine lifeEngine;
lifeEngine.globalObject().setProperty("direction", lifeEngine.toScriptValue<QVector3D>(dir));

This QML code outputs nothing interesting:

console.log("1-!");    
console.log("2-"+direction);
console.log("3.1-"+direction.x);
//console.log("3.2-"+direction.x()); interrupt execution

for(var property in direction) {
    console.log("4-"+property) + ": "+direction[property];
}
console.log("5-!");

var v = Qt.vector3d(1,2,3);
console.log("6-!" + v);

Output:

qml: 1-!
qml: 2-QVariant(QVector3D)
qml: 3.1-undefined
qml: 5-!
qml: 6-!undefined

I tried to import in file

import QtQuick 2.1

but it interrupt execution.

How can i get access to value QVector3D in QML? May be some imports in cpp project or in script file?... Problem is in environment not in code. May be vector3d available only in quick applications.

My *.pro file:

QT       += core gui multimedia qml quick

(Qt 5.12.2)

morpheus
  • 31
  • 3
  • C++/Qt types converted to an appropriate javascript/QML types, see the conversion table [here](https://doc.qt.io/qt-5/qtqml-cppintegration-data.html) – folibis Nov 19 '20 at 11:45
  • Thanks. I read it. But question is still active. How to get QVector3D from QVariant? – morpheus Nov 19 '20 at 12:14
  • There is no `QVector3D` type in QML, you cannot get it. While passing from C++ to QML the QVector3D value converted to [vector3d](https://doc.qt.io/qt-5/qml-vector3d.html) which has properties `x`, `y`, and `z` that you can use. – folibis Nov 19 '20 at 12:36
  • It's seems isn't right: i changed QML code - and i don' have access to x (method or field). vector3d is IN the QVariant. – morpheus Nov 19 '20 at 13:21
  • Problem is in environment not in code. May be vector3d available only in quick applications – morpheus Nov 19 '20 at 13:55
  • As I can see from your output `console.log("2-"+direction); -> qml: 2-QVariant(QVector3D)`, you have been able to translate the object from Qt (C++) to Js (Javascript) successfully. So you can inspect your js object now: `console.log(direction);` (i.e. don't try to convert your object to string, as it is a Javascript object - JSON). This will allow you to inspect the full object in Js console. – azbarcea Nov 19 '20 at 14:04

0 Answers0