本页内容

matrix4x4 QML Value Type

4×4 矩阵是一种由 4 行 4 列组成的矩阵。更多内容...

Import Statement: import QtQuick

详细说明

matrix4x4 类型包含十六个值,每个值均可通过QML中的m11 至m44 属性访问(按行/列顺序排列)。 该类型的值可通过 Qt.matrix4x4() 函数生成。matrix4x4 中的每个属性均以实数形式存储(ARM 架构下为单精度,x86 架构下为双精度)。

matrix4x4 类型的属性默认为单位矩阵,其对角线元素m11 、m22 、m33 和m44 均为1 ,其余所有元素均为0 。

matrix4x4 类型具有以下可在 QML 中调用的幂等函数:

函数签名描述示例
translate(vector3d vector)将this 矩阵4x4与另一个矩阵相乘,该矩阵会根据vector
var m = Qt.matrix4x4();
m.translate(Qt.vector3d(1,2,3));
console.log(m.toString());
// QMatrix4x4(1, 0, 0, 1, 0, 1, 0, 2, 0, 0, 1, 3, 0, 0, 0, 1)
rotate(real angle, vector3d axis)将this 矩阵4x4与另一个矩阵相乘,该矩阵通过angle 度围绕axis
var m = Qt.matrix4x4();
m.rotate(180,Qt.vector3d(1,0,0));
console.log(m.toString());
// QMatrix4x4(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1)
rotate(四元数 四元数)将this 矩阵4x4与另一个矩阵相乘,该矩阵会根据指定的quaternion 对坐标进行旋转。假设quaternion 已归一化。
var m = Qt.matrix4x4();
m.rotate(Qt.quaternion(0.5,0.5,0.5,-0.5));
console.log(m.toString());
// QMatrix4x4(0, 1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, 0, 0, 1)
scale(real factor)将this 矩阵4x4与另一个矩阵相乘,该矩阵按给定的factor
var m = Qt.matrix4x4();
m.scale(2);
console.log(m.toString());
// QMatrix4x4(2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1)
scale(实数 x, 实数 y, 实数 z)将this 矩阵4x4与另一个矩阵相乘,该矩阵根据分量x 、y 和z
var m = Qt.matrix4x4();
m.scale(1,2,3);
console.log(m.toString());
// QMatrix4x4(1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1)
scale(vector3d vector)将this 的4x4矩阵与另一个矩阵相乘,该矩阵通过以下分量对坐标进行缩放: 、 以及scale(vector3d vector)将 的4x4矩阵与另一个矩阵相乘,该矩阵通过以下分量对坐标进行缩放:vector
var m = Qt.matrix4x4();
m.scale(Qt.vector3d(1,2,3));
console.log(m.toString());
// QMatrix4x4(1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1)
lookAt(vector3d eye, vector3d center, vector3d up)将this 矩阵4x4 与由eye 点推导出的视图矩阵相乘。center 向量3d 指示eye 所注视的视图中心。up 向量3d 指示相对于eye 应将哪个方向视为向上方向。
var m = Qt.matrix4x4();
m.lookAt(Qt.vector3d(1,2,3),Qt.vector3d(1,2,0),Qt.vector3d(0,1,0));
console.log(m.toString());
// QMatrix4x4(1, 0, 0, -1, 0, 1, 0, -2, 0, 0, 1, -3, 0, 0, 0, 1)
matrix4x4 times(matrix4x4 other)返回将this matrix4x4 与other matrix4x4 相乘所得的 matrix4x4 结果
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = Qt.matrix4x4(4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
var c = a.times(b);
console.log(c.toString());
// QMatrix4x4(120, 130, 140, 150, 280, 306, 332, 358, 440, 482,
//524, 566, 600, 658, 716, 774)
vector4d times(vector4d vector)返回将vector 根据this 矩阵4x4进行变换后得到的vector4d结果,其中矩阵在向量前应用
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = Qt.vector4d(5,6,7,8);
var c = a.times(b);
console.log(c.toString()); // QVector4D(70, 174, 278, 382)
vector3d times(vector3d vector)返回将向量vector 根据矩阵this matrix4x4进行变换后得到的vector3d结果,其中矩阵在向量前应用vector3d times(vector3d vector)
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = Qt.vector3d(5,6,7);
var c = a.times(b);
console.log(c.toString()); // QVector3D(0.155556, 0.437037, 0.718518)
matrix4x4 times(real factor)返回将this 矩阵4x4 与标量相乘后得到的矩阵4x4 结果factor
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = 4.48;
var c = a.times(b);
console.log(c.toString());
// QMatrix4x4(4.48, 8.96, 13.44, 17.92, 22.4, 26.88, 31.36, 35.84,
// 40.32, 44.8, 49.28, 53.76, 58.24, 62.72, 67.2, 71.68)
matrix4x4 plus(matrix4x4 other)返回this 矩阵与other 矩阵相加后的 4x4 矩阵结果
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = Qt.matrix4x4(5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
var c = a.plus(b);
console.log(c.toString());
// QMatrix4x4(6, 8, 10, 12, 14, 16, 18, 20, 22,
// 24, 26, 28, 30, 32, 34, 36)
matrix4x4 minus(matrix4x4 other)返回从this 矩阵4x4中减去other 矩阵4x4后得到的矩阵4x4结果
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = Qt.matrix4x4(5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
var c = a.minus(b);
console.log(c.toString());
// QMatrix4x4(-4, -4, -4, -4, -4, -4, -4, -4, -4,
// -4, -4, -4, -4, -4, -4, -4)
vector4d row(int which)返回由which 指定的this 的行向量4d。注意:which 表示以0为起点的矩阵索引。
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = Qt.vector4d(a.m21, a.m22, a.m23, a.m24);
var c = a.row(2); // zero based access!  so not equal to b
console.log(b.toString() + " " + c.toString());
// QVector4D(5, 6, 7, 8) QVector4D(9, 10, 11, 12)
vector4d column(int which)返回由 `which` 指定的 `this ` 的 `vector4d` 行。注意:which 表示以 0 为起点的矩阵索引。
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = Qt.vector4d(a.m12, a.m22, a.m32, a.m42);
var c = a.column(2); // zero based access!  so not equal to b
console.log(b.toString() + " " + c.toString());
// QVector4D(2, 6, 10, 14) QVector4D(3, 7, 11, 15)
real determinant()返回this 矩阵4x4的行列式
var a = Qt.matrix4x4(1,0,0,0,0,2,0,0,0,0,3,0,100,200,300,1);
var b = a.determinant();
console.log(b); // 6
matrix4x4 inverted()返回this matrix4x4 的逆矩阵(若存在),否则返回单位矩阵。
var a = Qt.matrix4x4(1,0,0,0,0,2,0,0,0,0,3,0,100,200,300,1);
var b = a.inverted();
console.log(b.toString());
// QMatrix4x4(1, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0.333333, 0, -100,
// -100, -100, 1)
matrix4x4 transposed()返回this 矩阵4x4的转置
var a = Qt.matrix4x4(1,0,0,0,0,2,0,0,0,0,3,0,100,200,300,1);
var b = a.transposed();
console.log(b.toString());
// QMatrix4x4(1, 0, 0, 100, 0, 2, 0, 200, 0, 0, 3, 300, 0, 0, 0, 1)
rect mapRect(rect)将给定的矩形映射到由该矩阵定义的坐标系中。如果指定了旋转或剪切,则该函数返回边界矩形。该函数在 Qt 6.5 中引入。
var a = Qt.matrix4x4(2,0,0,0,0,2,0,0,0,0,1,0,0,0,0,1);
var b = a.mapRect(Qt.rect(10, 20, 30, 40));
console.log(b.toString());
// Qt.rect(20, 40, 60, 80)
point map(point)将给定的点映射到由该矩阵定义的坐标系中。该函数在 Qt 6.5 中引入。
var a = Qt.matrix4x4(2,0,0,0,0,2,0,0,0,0,1,0,0,0,0,1);
var b = a.map(10, 20);
console.log(b.toString());
// Qt.point(20, 40)
bool fuzzyEquals(matrix4x4 other, real epsilon)如果矩阵this 与矩阵other 近似相等,则返回true。当矩阵this 的每个属性均在矩阵other 相应属性的epsilon 范围内时,该近似关系成立。请注意,epsilon 是可选参数,默认值epsilon 为0.00001。
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = Qt.matrix4x4(1.0001,2.0001,3.0002,4.0003,5.0001,6.0002,
                     7.0002,8.0004, 9.0001,10.0003,
                     11.0003,12.0004,13.0001,
                     14.0002,15.0003,16.0004);
var c = a.fuzzyEquals(b);        // default epsilon
var d = a.fuzzyEquals(b, 0.005); // supplied epsilon
console.log(c + " " + d); // false true

此值类型由QtQuick 导入提供。

另请参阅 QML 值类型。

© 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.