Android のフラグメントQt Quick for Android

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

QtQuickViewAPIに慣れていない場合は、このチュートリアルを続ける前にドキュメントを読んでください。

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

  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. オーバーロードされたonCreateView() 関数の内部で宣言します:
    1. QtQuickViewにアクティビティコンテキストを割り当てます。this.Activity
      homeQtQuickView = QtQuickView(this.activity)
      homeQmlContent = Screen01()
    2. レイアウトパラメータをFrameLayout.LayoutParamsで初期化します。
      val params = FrameLayout.LayoutParams(
         ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
      
      binding = FragmentHomeBinding.inflate(inflater, container, false)
      val root: View = binding.root
      \li Add it to the layout:
      \code
      binding.homeQmlFrame.addView(homeQtQuickView, params)
    3. QMLコンテンツをロードします:
      homeQtQuickView.loadContent(homeQmlContent)
      return root

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.