QML - cum pot obține comboBox.currentText de la un alt component?

0

Problema

Am început de învățare qml și au o întrebare cu privire la modul de a obține comboBox.currentText de o altă componentă.

exemplu de cod:

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
          }
       }
    }
}

-> Am nevoie pentru a obține selectate Listelement din Combobox în settingspage și să-l utilizați în componenta cu id: lastPage

Orice ajutor ar fi apreciat foarte mult

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

Cel mai bun răspuns

0

De fapt nu am nici o idee ce elemente sunt utilizați aici (App, NavigationStack etc.) dar, în general, struct poate fi, cum ar fi următoarele:

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"
                }
            }
        }
    }
}

La element1 depinde de o proprietate de părinte/primul element care întotdeauna în domeniul de aplicare. Această construcție numită property bindings în QML și poate fi făcut în mai multe mod (de exemplu, folosind Obligatoriu, bun pentru elemente dinamice ). La element2 de asemenea, puteți accesa/modifica proprietatea și deci schimba-l de la element2 afectează imediat element1. Care, de asemenea, rezolvă o problemă atunci când doriți să acces element1 din root și element1 nu în domeniul de aplicare, sau nu există, sau orice altceva va prăbuși cerere în acest caz.

2021-11-17 11:29:59

Window nu contează aici, am vrut doar să arăt concept, un exemplu de lucru
folibis

În alte limbi

Această pagină este în alte limbi

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