天天看點

Qt基于Qml實作底部導航欄(TabBar)

示範效果:

Qt基于Qml實作底部導航欄(TabBar)
Qt基于Qml實作底部導航欄(TabBar)
Qt基于Qml實作底部導航欄(TabBar)

 1.主要使用Tabbar控件實作

2.使用SVG圖檔進行顔色填充

3.使用QtGraphicalEffects庫中的ColorOverlay修改SVG顔色

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtGraphicalEffects 1.12
TabBar {
    property alias myModel: idTabbarModel
    property int lastIndex: 0
    id: idTabbar
    currentIndex: 0
    ListModel {id: idTabbarModel}
    Repeater {
        id: idRepeaterControl
        model: idTabbarModel
        TabButton {
            property alias imageSource: image.source //圖像
            property alias textColor: text.color //文本
            height: idTabbar.height
            contentItem:Text{
                id: text
                text: modelText
                font.pixelSize: 24
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignBottom
                color: (model.index === idTabbar.currentIndex) ? modelColorG : modelColor
            }

            background:Image{
                id: image
                width: 48
                height: 48
                anchors.horizontalCenter: parent.horizontalCenter
                source: (model.index === idTabbar.currentIndex) ? modelSrcG : modelSrc
                ColorOverlay{
                            anchors.fill: image
                            source: image
                            color: "red"
                        }

            }
            //滑鼠經過事件處理
            onHoveredChanged: {
                if (model.index !== idTabbar.currentIndex){
                    hovered ? text.color = modelColorG : text.color = modelColor
                    hovered ? image.source = modelSrcG : image.source = modelSrc
                }
            }
            //點選事件處理
            onClicked: {
                idRepeaterControl.itemAt(idTabbar.lastIndex).imageSource = myModel.get(idTabbar.lastIndex).modelSrc;
                idRepeaterControl.itemAt(idTabbar.lastIndex).textColor = modelColor;
                image.source = modelSrcG;
                text.color = modelColorG;
                idTabbar.lastIndex = model.index;
            }
        }
    }
}      
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtQuick.Window 2.2

ApplicationWindow {
    id: frmWindow
    visible: true
    width: 600
    height: 800
    title: qsTr("Qt基于Qml實作底部導航欄(TabBar)")

    footer:Rectangle{
        color:"blue"
        height:80
        BaseTabBar{
        id: bar
        height: 80
        width: parent.width
        anchors.bottom: parent.bottom
        Component.onCompleted: {
            myModel.append({ "modelText": "消息", "modelColor": "red", "modelColorG": "#148014",
                               "modelSrc": "qrc:/images/Chat_MsgRecord.svg", "modelSrcG": "qrc:/images/Chat_MsgRecordG.svg"})
            myModel.append({ "modelText": "聯系人", "modelColor": "red", "modelColorG": "#148014",
                               "modelSrc": "qrc:/images/Chat_FriendManager.svg", "modelSrcG": "qrc:/images/Chat_FriendManagerG.svg"})
            myModel.append({ "modelText": "發現", "modelColor": "red", "modelColorG": "#148014",
                               "modelSrc": "qrc:/images/Mobile_Find.svg", "modelSrcG": "qrc:/images/Mobile_FindG.svg"})
            myModel.append({ "modelText": "我", "modelColor": "red", "modelColorG": "#148014",
                               "modelSrc": "qrc:/images/Main_P2PChat.svg", "modelSrcG": "qrc:/images/Main_P2PChatG.svg"})
        }
    }
    }


    SwipeView {
        id: view
        height: frmWindow.height - 120
        width: parent.width
        currentIndex: bar.currentIndex
        anchors.top: topbar.bottom

        Rectangle{
            color:"red"
            Text {
                text: qsTr("消息")
                color:"white"
                font.pixelSize: 30
                anchors.centerIn: parent
            }
        }
        Rectangle{
            color:"green"
            Text {
                text: qsTr("聯系人")
                color:"white"
                font.pixelSize: 30
                anchors.centerIn: parent
            }
        }
        Rectangle{
            color:"blue"
            Text {
                text: qsTr("發現")
                color:"white"
                font.pixelSize: 30
                anchors.centerIn: parent
            }
        }
        Rectangle{
            color:"yellow"
            Text {
                text: qsTr("我")
                color:"red"
                font.pixelSize: 30
                anchors.centerIn: parent
            }
        }
    }
}      

繼續閱讀