QML - ¿cómo puedo obtener comboBox.currentText de otro componente?

0

Pregunta

Acabo de comenzar el aprendizaje qml y tengo una pregunta sobre cómo obtener el comboBox.currentText de otro componente.

ejemplo de código:

App {
    id: app
    width: px(250); height: px(250)

    NavigationStack {
        Page {
            id: page
            navigationBarHidden: true
            AppText { text: "startpage" }
            SimpleButton{
                x: 220; y: 0; onClicked: page.navigationStack.push(settingsPage)
            }
            AppText {
                x: 0; y: 50; text: "text " + comboBox1.currentText
            }
        }
    }

    Component {
        id: settingsPage
        Page {
          navigationBarHidden: true
          AppText { text: qsTr("settings page") }
          SimpleButton{
              x: 220; y: 0; onClicked: page.navigationStack.push(lastPage)
          }

          ComboBox {
              id: comboBox1
              currentIndex: 0
              x: 10; y: 40
              style: ComboBoxStyle {
                  label: Text {
                      text: control.currentText
                  }
              }
              model: ListModel {
                  ListElement { text: "green" }
                  ListElement { text: "dark-green" }
                  ListElement { text: "blue" }
              }
          }

          AppText {
              x: 0; y: 90; text: "text " + comboBox1.currentText
          }
        }
    }

    Component {
        id: lastPage
        Page {
          navigationBarHidden: true
          AppText { text: qsTr("last page") }
          SimpleButton{
              x: 220; y: 0; onClicked: page.navigationStack.push(page)
          }
          AppText {
              x: px(50); y: px(90); text: "text " + comboBox1.currentText
          }
       }
    }
}

-> Necesito conseguir el seleccionado Listelement desde el Combobox en settingspage y utilizarlo en el componente con id: lastPage

Cualquier ayuda sería muy apreciada

combobox components embedded qml
2021-11-17 09:11:20
1

Mejor respuesta

0

En realidad no tengo ni idea de lo que son los elementos que utiliza (App, NavigationStack etc.) pero, en general, la estructura puede ser como la siguiente:

Window {
    visible: true   
    width: 400
    height: 300
    id: root
    property color someColor: "green"

    Row {
        id: element1
        anchors.centerIn: parent
        Rectangle {
            width: 100
            height: 100
            color: root.someColor
        }

        Rectangle {
            id: element2
            width: 100
            height: 100
            color: "yellow"
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    root.someColor = "blue"
                }
            }
        }
    }
}

El element1 depende de la propiedad de su padre/elemento que siempre en el ámbito de aplicación. Esta construcción llamada property bindings en QML y se puede hacer en varias maneras (por ejemplo utilizando el Enlace, bueno para la dinámica de los elementos ). El element2 también puede acceder a/cambio de la propiedad y por lo que el cambio de element2 inmediatamente afecta element1. Que también resuelve un problema cuando se quiere acceder element1 a partir de la root y element1 no en el ámbito de aplicación, o no existe o cualquier otra cosa que se bloqueará la aplicación en este caso.

2021-11-17 11:29:59

Window no importa aquí, yo sólo quería mostrar el concepto, un ejemplo de trabajo
folibis

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................