このページでは

Qt 3D:基本形状のC++サンプル

Qt 3D が提供する4つの基本形状を表示し、それぞれに対してメッシュを設定します。

4つの3Dオブジェクト(トーラス、円柱、直方体、球体)が表示されたスクリーンショット

「基本形状」では、Qt 3D が提供する4つの基本形状(トーラス、円柱、立方体、球体)を紹介します。また、この例では、Qt 3D のシーンをウィジェットに埋め込み、他のウィジェットと接続する方法についても説明しています。

例の動作

このサンプルは、以下の手順で実行できます。

トーラスメッシュの設定

例として、トーラスメッシュの設定方法を説明します。まず、QTorusMesh をインスタンス化し、次にメッシュ固有のパラメータ(トーラスの場合は半径、小半径、リング数、スライス数)を設定します。

m_torus = new Qt3DExtras::QTorusMesh();
m_torus->setRadius(1.0f);
m_torus->setMinorRadius(0.4f);
m_torus->setRings(100);
m_torus->setSlices(20);

トーラスのサイズと位置は、トランスフォームコンポーネントを使用して調整できます。スケール、トランスレーション、および回転のコンポーネントを作成し、それらをQTransform コンポーネントに追加します。

Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform();
torusTransform->setScale(2.0f);
torusTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0.0f, 1.0f, 0.0f), 25.0f));
torusTransform->setTranslation(QVector3D(5.0f, 4.0f, 0.0f));

メッシュの拡散色を変更するには、QPhongMaterial を作成し、その拡散色を設定します。

Qt3DExtras::QPhongMaterial *torusMaterial = new Qt3DExtras::QPhongMaterial();
torusMaterial->setDiffuse(QColor(QRgb(0xbeb32b)));

最後のステップは、トーラスをエンティティツリーに追加することです。これを行うには、親エンティティを持つQEntity を作成し、先に作成したメッシュ、マテリアル、およびトランスフォームコンポーネントをそこに追加します。

m_torusEntity = new Qt3DCore::QEntity(m_rootEntity);
m_torusEntity->addComponent(m_torus);
m_torusEntity->addComponent(torusMaterial);
m_torusEntity->addComponent(torusTransform);

エンティティの可視性は、親を持つかどうか、つまりエンティティツリーの一部であるかどうかを定義することで制御できます。

void SceneModifier::enableTorus(bool enabled)
{
    m_torusEntity->setEnabled(enabled);
}

サンプルプロジェクト @ code.qt.io

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