当前位置:编程学习 > wap >>

求解QML画面加载时出现的一个问题

下面的代码是一个LISTVIEW,想实现滑动,并点击其中一条内容进行页面加载条跳转。
但执行的时候出现了错误,最后自定义信号 changepage()的地方,QT提示找不到变量b,但b明明已经定义了阿,刚接触QT很多不懂,哪位大神给解答一下吧,谢谢了。

import QtQuick 1.1

    Rectangle {
        id: rec2
        width: 273
        height: 213
        radius: 5
        border.color: "lightgrey"
        anchors.centerIn: parent
        signal changepage()

        ListView {
            id: listview1
            anchors.fill: parent
            width: rec2.width
            height: rec2.height
            model: listviewmodel1
            delegate: listviewdele1
            clip: true
        }

        Component {
            id:listviewdele1
            Item {
                id: rec3
                width: listview1.width
                height: listview1.height / 5
                Rectangle {
                    id: rec8
                    width: rec3.width - 1
                    height: rec3.height - 1
                    anchors.centerIn: rec3
                    color: "lightgrey"
                    visible: false
                }
                MouseArea {
                    anchors.fill: rec3
                    onPressed: rec8.visible = true
                    onReleased: rec8.visible = false
                    onPositionChanged: rec8.visible = false
                    onClicked: changepage()
                }
                Item {
                    id: rec4
                    anchors.left: rec3.left
                    anchors.top: rec3.top
                    width: rec3.width * 3 / 5
                    height: rec3.height * 2 / 3
                    Text {
                        id: a
                        text: ssid
                        font.pixelSize: 18
                        anchors.left: parent.left
                        anchors.leftMargin: 10
                        anchors.top: parent.top
                        anchors.topMargin: 5
                        anchors.horizontalCenterOffset: -10
                        color: "#049fc7"
                        font.family: "Arial"
                    }
                }
                Item {
                    id: rec5
                    anchors.top: rec4.bottom
                    anchors.left: rec3.left
                    width: rec3.width * 3 / 5
                    height: rec3.height / 3
                    Text {
                        id: b
                        text: ssid_info
                        font.pixelSize: 10
                        anchors.left: parent.left
                        anchors.leftMargin: 10
                        anchors.horizontalCenterOffset: -10
                        color: "#049fc7"
                        font.family: "Arial"
                    }
                }
                Item {
                    id: rec6
                    anchors.left: rec4.right
                    anchors.top: rec3.top
                    width: rec3.width / 5
                    height: rec3.height
                    Image {
                        id: c
                        source: csource
                        anchors.centerIn: parent
                    }
                }
                Item {
                    id: rec7
                    anchors.left: rec6.right
                    anchors.top: rec3.top
                    width: rec3.width / 5
                    height: rec3.height
                    //border.color: "lightgrey"
                    Image {
                        id: d
                        source: dsource
                        anchors.centerIn: parent
                    }
                }
            }
        }
        ListModel {
            id: listviewmodel1
            ListElement{
                ssid: "Shady"
                ssid_info: "接続した"
                csource: "images/openlock.png"
                dsource: "images/singl.jpg"
            }
            ListElement{
                ssid: "TP-LINK-26615C"
                ssid_info: "WPA PSKにより保護されている"
                csource: "images/closelock.png"
                dsource: "images/singl.jpg"
            }
            ListElement{
                ssid: "TP-LINK-26615C"
                ssid_info: "WPA PSKにより保護されている"
                csource: "images/closelock.png"
                dsource: "images/singl.jpg"
            }
            ListElement{
                ssid: "TP-LINK-215C"
                ssid_info: "WPA PSKにより保護されている"
                csource: "images/closelock.png"
                dsource: "images/singl.jpg"
            }
            ListElement{
                ssid: "TP-LIN12K-26615C"
                ssid_info: "WPA PSKにより保護されている"
                csource: "images/closelock.png"
                dsource: "images/singl.jpg"
            }
            ListElement{
                ssid: "TP-23K-26615C"
                ssid_info: "WPA PSKにより保護されている"
                csource: "images/closelock.png"
                dsource: "images/singl.jpg"
            }
            ListElement{
                ssid: "3123123"
                ssid_info: "WPA PSKにより保護されている"
                csource: "images/closelock.png"
                dsource: "images/singl.jpg"
            }
        }
        onChangepage: {
            if(b.text == "接続した")
            {
                nextpage.source = "WiFi_Sta_Connected_Information.qml"
            }
            else
            {
                nextpage.source = "WiFi_Sta_Connect_Setup.qml"
            }
        }
    }
--------------------编程问答-------------------- b是一个动态创建的对象,在Component之外是无法被访问的

可以有两种办法解决,
1. 把onChangepage中的代码拷贝到MouseArea的onClicked中,类似下面的代码

MouseArea {
    anchors.fill: rec3
    onPressed: rec8.visible = true
    onReleased: rec8.visible = false
    onPositionChanged: rec8.visible = false
    onClicked: {
        if(b.text == "接続した")
        {
            nextpage.source = "WiFi_Sta_Connected_Information.qml"
        }
        else
        {
            nextpage.source = "WiFi_Sta_Connect_Setup.qml"
        }
    }
}

2. 为changepage信号增加一个参数,类似下面的代码:

......
signal changepage(string b)
......
onClicked: changepage(b.text)
......
onChangepage: {
    if(b == "接続した")
......

--------------------编程问答-------------------- 问题解决了,非常感谢!

还想问一个问题:Component里面有个鼠标事件MouseArea,ID为ma1

我现在想把它做一个别名定义出来,然后在其他的QML里面去定义状态。

property alias mouseAreaEnable: ma1.enabled

但应该是类似上面的问题,因为是Component里的,在外面无法被访问。
错误信息为:Invalid alias reference. Unable to find id "ma1"

请问这里应该如何处理?谢谢了。
--------------------编程问答-------------------- 使用ListView的currentItem可以访问 --------------------编程问答--------------------
补充:移动开发 ,  Qt
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,