QML之TabWidget

    //TabWidget.qml  
    import Qt 4.7  
    Item {  
        id: tabWidget  
        default property alias content: stack.children //将tab页集合设置一个默认属性  
        property int current: 0  
        onCurrentChanged: setOpacities()  
        Component.onCompleted: setOpacities()  
        function setOpacities() {  
            for(var i = 0; i < content.length; ++i) {  
                content[i].opacity = (i == current ? 1 : 0);//将当前的tab设置为非透明,其余透明  
            }  
        }  
        Row {  //此组件为tab选项  
            id: header  
            Repeater {  
                model: content.length  
                delegate: Rectangle {  
                    width: tabWidget.width / content.length  
                    height: 36  
                    color: "#e3e3e3"  
                    Rectangle {  //此组件为tab选项和tab页之间的一条线  
                        width: tabWidget.width; height: 1  
                        anchors { bottom: parent.bottom; bottomMargin: 1}  
                        color: "#abc2c2"  
                    }  
                    BorderImage {  //tab选项图片  
                        anchors { fill: parent; leftMargin: 2; topMargin: 5; rightMargin: 1}  
                        border { left: 7; right: 7}  
                        source: tabWidget.current == index? "tab.png" : "unselect.png"  
                    }  
                    Text {  //tab选项的文本  
                        horizontalAlignment: "AlignHCenter"; verticalAlignment: "AlignVCenter"  
                        anchors.fill: parent  
                        text: content[index].title  
                        elide: Text.ElideRight  
                        font.bold: tabWidget.current == index  
                    }  
                    MouseArea {  
                        anchors.fill: parent  
                        onClicked: tabWidget.current = index  //存储当前选中tab页  
                    }  
                }  
      
            }  
        }  
        Item {  //此组件为tab页  
            id: stack  
            width: tabWidget.width  
            anchors.top: header.bottom  
            anchors.bottom: tabWidget.bottom  
        }  
      
    }  

    //main.qml  
    import Qt 4.7  
    TabWidget {  
        id: tabs  
        width: 640; height: 480  
        Rectangle {  //第一个tab页  
            property  string title: "Red"  
            anchors.fill: parent  
            color: "#e3e3e3"  
            Rectangle {  
                anchors.fill: parent; anchors.margins: 20 //tab边框与父容器的距离  
                color: "#7fff7f"  
                Text {  
                    width: parent.width - 20  
                    anchors.centerIn: parent; horizontalAlignment: "AlignHCenter"  
                    text: "Roses are red"  
                    font.pixelSize: 20  
                    wrapMode: Text.WordWrap  
                }  
            }  
        }  
        Rectangle { //第二个tab页  
            property  string title: "Green"  
            anchors.fill: parent  
            color: "#e3e3e3"  
            Rectangle {  
                anchors.fill: parent; anchors.margins: 20  
                color: "#7fff7f"  
                Text {  
                    width: parent.width - 20  
                    anchors.centerIn: parent; horizontalAlignment: "AlignHCenter"  
                    text: "Flower stems are green"  
                    font.pixelSize: 20  
                    wrapMode: Text.WordWrap  
                }  
            }  
        }  
        Rectangle { //第三个tab页  
            property  string title: "Blue"  
            anchors.fill: parent  
            color: "#e3e3e3"  
            Rectangle {  
                anchors.fill: parent; anchors.margins: 20  
                color: "#7fff7f"  
                Text {  
                    width: parent.width - 20  
                    anchors.centerIn: parent; horizontalAlignment: "AlignHCenter"  
                    text: "Violets are blue"  
                    font.pixelSize: 20  
                    wrapMode: Text.WordWrap  
                }  
            }  
        }  
    }  


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值