Android のフラグメントQt Quick for Android
ViewGroupベースのオブジェクトを使用することで、AndroidのUIレイアウトにQtQuickViewを配置することができます。ここでは、FrameLayoutを使用します。
QtQuickViewAPIに慣れていない場合は、このチュートリアルを続ける前にドキュメントを読んでください。
先に進む前に、Qt アカデミーのコース「EmbeddingQt Quick 3D Content in an Android App」を読んでおきましょう。
はじめに、Bottom Navigation Views Activity テンプレートを使って Android Studio で新しいプロジェクトを作成します。
- QtQuickViewを表示させたいフラグメントまたはアクティビティを探します。ここでは、
HomeFragment
とfragment_home.xml
を使用します。 fragment_home.xml
、FrameLayoutを作成し、id
。<FrameLayout android:id="@+id/homeQmlFrame" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHeight_percent="0.8"/>
このidは、レイアウトをバインドするときに
HomeFragment
で参照する必要があるので、メモしておいてください。- HomeFragment.ktの中に、FrameLayoutのimport文を追加します:
import android.widget.FrameLayout
- QtQuickViewとScreen01 QMLタイプのインポートを追加し、クラス内で宣言します:
import org.qtproject.qt.android.QtQuickView import org.qtproject.example.RoboApp.RoboContent.Screen01 class HomeFragment : Fragment() { private var binding: FragmentHomeBinding? = null private lateinit var homeQmlContent: Screen01 private lateinit var homeQtQuickView: QtQuickView
- QtQuickViewを割り当て、アクティビティインスタンスとして
requireActivity()
homeQtQuickView = QtQuickView(requireActivity()) homeQmlContent = Screen01()
- プログラムでビューを作成したり、動的にビューを追加したり、実行時に変更する場合は、レイアウトパラメータを初期化します。そうでない場合は、このセクションをスキップすることができ、
addView()
でparams
を使用する必要はありません。val params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
- ビューをレイアウトに追加します:
- View bindinginside
onCreateView()
: まず、アプリのbuild.gradle.kts androidセクションにbuildFeature
セクションを追加して、ビューバインディングが有効になっていることを確認します:buildFeatures { viewBinding = true }
次に、
onCreateView()
に以下を追加します:binding = FragmentHomeBinding.inflate( inflater, container, false) homeQtQuickView.loadContent(homeQmlContent) val root: View = binding.root binding.homeQmlFrame.addView(homeQtQuickView, params) ... return root
- Using
findViewById()
insideonCreate()
:val qtFrame = findViewById(R.id.qtFrame) qmlFrame.addView(m_quickView, params) m_quickView.loadContent(homeQmlContent)
- View bindinginside
Qt Quick のコンテンツがホームフラグメントに表示されます。
© 2025 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.