QML-ได้ยังไงฉันเอากล่องคอมโบ.currentText จากอีกส่วนประกอบ?

0

คำถาม

ฉันเพิ่งเริ่มเรียนรู้ qml และมีคำถามว่าจะเอากล่องคอมโบ.currentText จากอีกส่วนประกอบ.

รหัสตัวอย่าง:

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

->ฉันต้องการให้คอนที่เลือกไว้ออ Listelement อกจากกล่องคอมโบนใน settingspage และใช้มันในส่วนประกอบหมายเลข:lastPage

ที่เขาแนะนำให้ผูกผ้ากันเปื้อนจะเป็น greatly รู้สึกขอบคุณ

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

คำตอบที่ดีที่สุด

0

ที่จริงฉันไม่ต้องสงสัยเลยพวกเขาเป็นส่วนประกอบที่คุณใช้อยู่(แอพฯ,NavigationStack etc.) แต่ในอนายพลค struct สามารถเป็นเหมือนต่อไปนี้:

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

คน element1 ขึ้นอยู่กับบาทรัพย์สินจากพ่อแม่ของมัน/ด้านบนรายการนั้นเสมออยู่ในขอบเขต. นี่เขตก่อสร้างชื่อ property bindings ใน QML และสามารถทำได้ในหลายทาง(ตัวอย่างเช่นการใช้ เชื่อมที่ดีสำหรับปรับอัตโนมัติส่วนประกอบ). คน element2 ยังสามารถเข้าใช้งาน/เปลี่ยนทรัพย์สินและการเปลี่ยนมันออกมาจาก element2 ทันทีมีผลกระทบต่ element1. นั่นยังแก้ปัญหาตอนที่คุณต้องการเพื่อเข้าใช้งาน element1 จาก root แล้ว element1 ไม่ได้อยู่ในขอบเขตหรือไม่มีอยู่จริงหรืออะไรก็ตามที่มันจะตกคุณโปรแกรมในคดีนี้

2021-11-17 11:29:59

Window ไม่สำคัญหรอยู่ที่นี่ฉันแค่อยากจะแสดงคอนเซ็ปต์ทำงานเป็นตัวอย่างเช่น
folibis

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่