Android Studio 向け Android でのQt Quick を使用したフラグメント

ViewGroup ベースのオブジェクトを使用することで、Android UI レイアウト内にQtQuickViewを配置できます。ここではFrameLayout を使用します。

QtQuickViewAPI に不慣れな場合は、このチュートリアルを進める前に、そのドキュメントをお読みください。

先に進む前に、Qt Academyのコース「Qt Academy: AndroidアプリへのQt Quick 3Dコンテンツの組み込み」を学習しておくことをお勧めします。

まず、Android Studioで「Bottom Navigation Views Activity 」テンプレートを使用して新しいプロジェクトを作成します。

  1. QtQuickView を表示したい Fragment または Activity を探します。ここでは、HomeFragment およびfragment_home.xml を使用します。
  2. 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 内でこのIDを参照する必要があるためです。

  3. HomeFragment.kt 内で、FrameLayout の import 文を追加します:
    import android.widget.FrameLayout
  4. 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
  5. QtQuickView を割り当て、Activity インスタンスを次のように指定します:requireActivity()
    homeQtQuickView = QtQuickView(requireActivity())
    homeQmlContent = Screen01()
  6. レイアウトパラメータを初期化します。ビューをプログラムで作成したり、動的に追加したり、実行時に変更したりする場合は、このセクションをスキップし、`params ` を `addView()` と共に使用する必要はありません。
    val params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                          ViewGroup.LayoutParams.MATCH_PARENT)
  7. 以下のいずれかの方法で、ビューをレイアウトに追加します。
    1. 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
    2. onCreate() 内でfindViewById() を使用する:
      val qtFrame = findViewById(R.id.qtFrame)
      qmlFrame.addView(m_quickView, params)
      m_quickView.loadContent(homeQmlContent)

      Android向けの他のQt Quick のサンプルで、その使用方法を確認してください。

これで、Qt Quick のコンテンツがホームフラグメントに表示されるようになります。

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