如何在Qt 5.2 QML应用程序中实现分页

时间:2014-02-22 20:22:41

标签: pagination qml q

我正在尝试创建一个一次显示3个页面的应用程序。中央页面显示3个显示sql查询结果1-3的矩形,左页是从查询结果4到图像的链接,右页也是从同一查询构建的,并且具有不同的布局。现在我无法将这3个页面放入listmodel并使用pathview使其看起来像一个paginator,因为所有3个页面都是不一致的而不是真正的模型,因此给我错误ListElement:不能包含嵌套元素。我正在粘贴下面的代码。我想要的只是让用户能够在页面之间轻弹,无论是涉及路径视图还是状态转换以及模仿翻页的体面转换:

import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
import "content"


Rectangle{
    property ListModel mainModel
    id: tripleView
    visible: true
    width: 800
    height: 1400

    PathView {
        model: mainModel
        delegate: mainDelegate
        id: paginator
        preferredHighlightBegin: 0.5
        preferredHighlightEnd: 0.5

        anchors.fill: parent
        path: Path {

            startX: -tripleView.width * mainModel.count / 2 + tripleView.width / 2;
            startY: tripleView.height / 2
            PathLine {
                x: tripleView.width * mainModel.count /2 + tripleView.width / 2
                y: tripleView.height * .5 }
        }
    }

    Component {
        id: mainDelegate

        Rectangle {
        width: tripleView.width
        height: tripleView.height
        }
    }

    ListModel {
        id: regionsModel
        ListElement {

            name: "Stomach"
        }
        ListElement {
            name: "Lung"
        }
        ListElement {
            name: "Colorectal"
        }
        ListElement {
            name: "Pancreas"
        }
        ListElement {
            name: "Urinary Bladder"
        }

    }

    ListModel {
        id: mainModel
        ListElement{
            Rectangle{
                id: tnmPage
                width: parent.width
                height: container.height


                Rectangle {
                    id: menu
                    z: 2
                    width: parent.width  ;
                    height: 75



                    Component {

                        id: appDelegate
                        Rectangle {
                            width: genericText.width + 10; height: parent.height
                            id: wrapper
                            color: PathView.isCurrentItem ? "yellow" : "white"
                            Text {
                                id: genericText
                                height: parent.height
                                font.pointSize: 12
                                verticalAlignment: Text.AlignVCenter
                                //                anchors.topMargin: wrapper.top
                                color: wrapper.PathView.isCurrentItem ? "red" : "black"
                                text: name
                            }

                            MouseArea {
                                //  width:parent.width; height: parent.height
                                anchors.fill: parent
                                hoverEnabled: true
                                onClicked: {
                                    var List = mWindow.printi(name)
                                    t.content = List[1]
                                    node.content = List[2]
                                    mets.content = List[3]
                                    view.currentIndex = index


                                }

                            }
                        }
                    }



                    PathView {
                        id: view
                        width: 2500
                        height: parent.height
                        anchors.rightMargin: 18
                        anchors.bottomMargin: 0
                        anchors.leftMargin: -18
                        anchors.topMargin: 0
                        anchors.fill: parent
                        //        anchors.bottom: parent.bottom
                        //        anchors.top: parent.top
                        //        preferredHighlightBegin: .1
                        //        preferredHighlightEnd: .6

                        highlightRangeMode: PathView.StrictlyEnforceRange
                        //        anchors.rightMargin: 0
                        //        anchors.bottomMargin: 0
                        //        anchors.leftMargin: 2
                        //        anchors.topMargin: -71

                        z: 1
                        highlight: Component {
                            Rectangle {
                                color: "yellow"
                                visible: PathView.onPath

                            }
                        }

                        focus: true
                        model: regionsModel
                        delegate: appDelegate

                        path: Path {

                            startX: 0; startY: view.height *.5
                            PathLine { x: menu.width; y: view.height * .5 }
                        }


                    }
                }



                Flickable {
                    id: flick

                    anchors.topMargin: menu.bottom
                    width: parent. width
                    height: parent. height - menu.height
                    contentWidth: parent.width+200
                    contentHeight: container.height // this is calculated on amount of text
                    PinchArea {
                        width: Math.max(flick.contentWidth, flick.width)
                        height: Math.max(flick.height, flick.height)
                        pinch.minimumScale: 0.5
                        pinch.maximumScale: 3

                        property real initialWidth
                        property real initialHeight
                        x: 0
                        y: 0
                        //![0]
                        onPinchStarted: {
                            initialWidth = flick.contentWidth
                            initialHeight = flick.contentHeight
                            flick.interactive = false
                        }
                        onPinchUpdated: {
                            t.fontSize = t.size*pinch.scale
                            node.fontSize = node.size * pinch.scale
                            mets.fontSize = mets.size * pinch.scale

                        }

                        onPinchFinished: {
                            flick.returnToBounds()
                            flick.interactive = true
                        }



                        Rectangle {
                            id: container

                            MouseArea {
                                anchors.fill: parent
                                onDoubleClicked: {

                                    t.fontSize = 12
                                    node.fontSize = t.size
                                    mets.fontSize = t.size
                                }


                            }


                            anchors.top: flick.top
                            width: parent.width
                            height: t.height + node.height + mets.height

                            StageBox {
                                id: t
                                anchors.top: container.top
                                color: "#6acbd3"
                            }

                            StageBox {
                                id: node
                                anchors.top: t.bottom
                                color: "#1fd77b"
                            }
                            StageBox {
                                id: mets
                                anchors.top: node.bottom
                                color: "blue"
                            }
                        }
                    }


                }
            }

}

我意识到上面的代码是冗长的,但我认为它会让我对我想要实现的目标有所了解。到目前为止我发现的例子有简单的页面显示图像,没有嵌套。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

尝试QML类型 VisualItemModel http://qt-project.org/doc/qt-4.8/qml-visualitemmodel.html#details。祝你好运

答案 1 :(得分:1)

我管理了一个黑客。在彩色矩形中进行实验性实现,这个可以在中间页面开始的3页应用程序,允许水平轻弹以访问相邻页面。它可以工作,我肯定通过一些调整它可以适合我的类似应用。邀请评论

import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0


Rectangle {
    state: "baseState"
    id: mainScreen
    width: 400
    height: 600


    Text {
        z:2
        id: logTxt
        height: 10
        width: 20
        function log(txt){
            text = txt+"\n"
        }
    }
    Flickable {
        boundsBehavior: StopAtBounds


        id: flick
        y: 48
        onContentXChanged: {
            if(flick.contentX>100 && mainScreen.state=="baseState" && flick.flickingHorizontally){
                logTxt.log(flick.contentX)
                mainScreen.state="State1"


            }


            if(flick.contentX<-100 && mainScreen.state=="baseState" && flick.flickingHorizontally){
                mainScreen.state="State2"
                logTxt.log(flick.contentX)

            }

            if(flick.contentX>100 && mainScreen.state=="State2" && flick.flickingHorizontally ){
                mainScreen.state="baseState"

                logTxt.log(flick.contentX)
                flick.contentX=0

            }


            if(flick.contentX<-100 && mainScreen.state=="State1" && flick.flickingHorizontally){
                logTxt.log(flick.contentX)
                mainScreen.state="baseState"


                flick.contentX=0

                logTxt.log(flick.contentX)
                Timer


            }

        }

        interactive: true
        width: 400
        height: 600
        transformOrigin: Item.Center
        flickableDirection: Flickable.HorizontalFlick

        Rectangle {
            width: 600

            height: 600
            id: container


            Rectangle {
                id:two
                visible: false
                x: 0
                z:3

                height: 600
                width: 400
                color: "grey"

            }
            Row{
                id: testGrid
                x:0
                visible: true


                y: 0
                z: 3

                width:600
                height: 600


                Rectangle {
                    id: a
                    anchors.top:parent.top

                    color:"#f6f7b1"
                    visible: true
                    z: 3
                    height: parent.height
                    width: 200
                }
                Rectangle {
                    id: b
                    anchors.top:parent.top

                    color:"#ffebeb"
                    visible: true
                    height: parent.height
                    width: 200
                }

                Rectangle {
                    id: c
                    y: -35
                    anchors.top:parent.top

                    color:"#b1d5f7"
                    height: parent.height
                    width: 200
                }

            }

            Rectangle {
                id: three
                visible: false
                z:2

                x:0
                height: parent.height
                width: 400
                color: "#028000"



            }



        }
    }
    states: [
        State {
            name: "State1"



            PropertyChanges {
                target: testGrid
                visible: false
            }

            PropertyChanges {
                target: two
                visible: true
            }

            PropertyChanges {
                target: three
                visible: false
            }
        },
        State {
            name: "State2"

            PropertyChanges {
                target: testGrid
                visible: false
            }

            PropertyChanges {
                target: three
                visible: true
            }
            PropertyChanges {
                target: two
                visible: false
            }

        },
        State {
            name: "baseState"


            PropertyChanges {

                target: testGrid
                visible: true
            }

            PropertyChanges {
                target: three
                visible: false
            }
            PropertyChanges {
                target: two
                visible: false
            }

        }
    ]
}