Fragmentos de Android con Qt Quick para Android
Puedes tener un QtQuickView en un layout UI de Android usando un objeto basado en ViewGroup. Aquí usaremos un FrameLayout.
Si no estás familiarizado con la API QtQuickView, lee su documentación antes de continuar con este tutorial.
Antes de continuar, vale la pena explorar el curso de Qt Academy, Embedding Qt Quick 3D Content in an Android App.
Para empezar, crea un nuevo proyecto en Android Studio utilizando la plantilla Bottom Navigation Views Activity.
- Localiza el Fragment o Activity bajo el cual quieres que tu QtQuickView sea visible. Aquí usamos
HomeFragmentyfragment_home.xml. - En
fragment_home.xmlcrea un FrameLayout y configura suidcomo se muestra a continuación.<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"/>
Tenga en cuenta este id, ya que necesita ser referenciado en
HomeFragmentcuando vincule el layout. - Dentro de HomeFragment.kt, añade una sentencia import para FrameLayout:
import android.widget.FrameLayout
- Añade tus importaciones para tus tipos QML QtQuickView y Screen01 y decláralos dentro de la clase:
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
- Asigna tu QtQuickView, dándole la instancia Activity usando
requireActivity()homeQtQuickView = QtQuickView(requireActivity()) homeQmlContent = Screen01()
- Inicializa los parámetros de layout, si creas vistas programáticamente, añades vistas dinámicamente o las cambias en tiempo de ejecución. En caso contrario puede saltarse esta sección y no necesita utilizar
paramsconaddView().val params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
- Añade tu vista al layout, ya sea con:
- Usando View binding dentro de
onCreateView(): Primero comprueba que view binding está habilitado añadiendo la secciónbuildFeatureen la sección build.gradle.kts android de tu app:buildFeatures { viewBinding = true }Luego añade lo siguiente en
onCreateView():binding = FragmentHomeBinding.inflate( inflater, container, false) homeQtQuickView.loadContent(homeQmlContent) val root: View = binding.root binding.homeQmlFrame.addView(homeQtQuickView, params) ... return root
- Usando
findViewById()dentro deonCreate():val qtFrame = findViewById(R.id.qtFrame) qmlFrame.addView(m_quickView, params) m_quickView.loadContent(homeQmlContent)
Vea el uso de otros ejemplos deQt Quick para Android.
- Usando View binding dentro de
Su contenido Qt Quick aparecerá ahora en su fragmento de inicio.
© 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.