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 で新しいプロジェクトを作成します。

  1. QtQuickViewを表示させたいフラグメントまたはアクティビティを探します。ここでは、HomeFragmentfragment_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 で参照する必要があるので、メモしておいてください。

  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を割り当て、アクティビティインスタンスとしてrequireActivity()
    homeQtQuickView = QtQuickView(requireActivity())
    homeQmlContent = Screen01()
  6. プログラムでビューを作成したり、動的にビューを追加したり、実行時に変更する場合は、レイアウトパラメータを初期化します。そうでない場合は、このセクションをスキップすることができ、addView()params を使用する必要はありません。
    val params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                          ViewGroup.LayoutParams.MATCH_PARENT)
  7. ビューをレイアウトに追加します:
    1. View bindinginsideonCreateView(): まず、アプリの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. UsingfindViewById() insideonCreate()
      val qtFrame = findViewById(R.id.qtFrame)
      qmlFrame.addView(m_quickView, params)
      m_quickView.loadContent(homeQmlContent)

      Androidの例については、他のQt Quick

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.