简体   繁体   English

Qt,QML ListView和桌面应用程序

[英]Qt, QML ListView and Desktop App

My question is kind of a two part conditional question. 我的问题是一个两部分条件问题。 I have a desktop application I'm writing in C++/Qt. 我有一个桌面应用程序,我用C ++ / Qt编写。 In the app I have a couple lists that I want to decorate and add list items with icons and rich text. 在应用程序中,我有几个列表,我想要装饰并添加带有图标和富文本的列表项。

I first attempted to do this with the QWidget world but the more I looked into it, the more I thought QML might be a better option. 我首先尝试使用QWidget世界做到这一点,但是我对它的研究越多,我就越认为QML可能是更好的选择。 But now I'm wondering about that as well since it seems that QML Is more geared toward touch screen devices. 但现在我也想知道这一点,因为看起来QML更适合触摸屏设备。 Not to mention that my progress with QML has been frusating. 更不用说我在QML方面取得的进展令人沮丧。 Give them QML below, I cannot figure out how to: (1) get an item to highlight when I click it and (2) add a scroll bar: 给他们下面的QML,我无法弄清楚如何:(1)当我点击它时得到一个项目突出显示(2)添加一个滚动条:

import QtQuick 1.0

Item
{
    width: 300
    height: 200

    ListModel
    {
        id: myModel2

        ListElement { text: "List Item 1" }
        ListElement { text: "List Item 2" }
        ListElement { text: "List Item 3" }
        ListElement { text: "List Item 4" }
        ListElement { text: "List Item 5" }
        ListElement { text: "List Item 6" }
    }

    Component
    {
        id: beerDelegate

        Rectangle
        {
            id: beerDelegateRectangle
            height: beerDelegateText.height * 1.5
            width: parent.width

            Text
            {
                id: beerDelegateText
                text: "<b>" + modelData + "</b> <i>(" + modelData + ")</i>"
            }

            MouseArea
            {
                anchors.fill: parent
                onClicked: 
                {
                    console.log("clicked: " + modelData + " at index: " + index);
                    beerList.currentIndex = index;
                }
            }
        }
    }

    ListView
    {
        id: beerList
        anchors.fill: parent
        model: myModel2
        delegate: beerDelegate

        highlightFollowsCurrentItem: true
        highlight: Rectangle
        {
            width: parent.width
            color: "red"
        }

        focus: true
    }
}

How can I accomplish what I'm looking for given this QML? 考虑到这个QML,我怎样才能完成我正在寻找的东西? Or is using QML in a QWidget desktop app just a bad idea all around? 或者在QWidget桌面应用程序中使用QML只是一个坏主意?

For the first question (highlight): 对于第一个问题(突出显示):

Your list actually draws the highlight, however, your item delegate overpaints this with a white rectangle! 您的列表实际上绘制了突出显示,但是,您的项目委托用白色矩形覆盖了它! Just replace the rectangle with an item and it works: 只需用项目替换矩形即可:

Component
{
    id: beerDelegate

    Item
    {
        ...
    }
}

For the second question (scroll bars): 对于第二个问题(滚动条):

As far as I know, QML doesn't provide scroll bars out of the box. 据我所知,QML不提供开箱即用的滚动条。 There is however the Qt Desktop Components project ( git repository ) which gives you access to most of the widgets in the QML world. 然而,有Qt桌面组件项目git存储库 ),它允许您访问QML世界中的大多数小部件。 Among them, there is a ScrollArea . 其中,有一个ScrollArea

It is no longer necessary to implement the Scrollbars yourself. 不再需要自己实现Scrollbars。 There is the ScrollView -Item since Qt 5.1 . Qt 5.1以来就有ScrollView -Item。 Simply surround a Flickable -Item (eg the ListView-Item you use, is also "Flickable") with the ScrollView-Item and you'll be fine: 只需使用ScrollView-Item环绕一个Flickable -Item(例如你使用的ListView-Item,也是“Flickable”)你就可以了:

ScrollView {   
    ListView {
        id: beerList
        anchors.fill: parent
        model: myModel2
        delegate: beerDelegate

        highlightFollowsCurrentItem: true
        highlight: Rectangle
        {
            width: parent.width
            color: "red"
        }

        focus: true
    }
}

For the second question. 对于第二个问题。 ie Scroll-bar on ListView : I have created code for scroll bar on ListView. ListView上的滚动条 :我已经为ListView上的滚动条创建了代码。 It also can work on the GridView 它也可以在GridView上运行

ScrollBar.qml ScrollBar.qml

import Qt 4.7

Item {
    property variant target

    width: 8
    anchors.top: target.top
    anchors.bottom: target.bottom
    anchors.margins: 1
    anchors.rightMargin: 2
    anchors.bottomMargin: 2
    anchors.right: target.right
    visible: (track.height == slider.height) ? false : true

    Image {
        id: scrollPath
        width: 2
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        source: "qrc:/resources/buttons/slider2.png"
    }

    Item {
        anchors.fill: parent

        Timer {
            property int scrollAmount

            id: timer
            repeat: true
            interval: 20
            onTriggered: {
                target.contentY = Math.max(0, Math.min(target.contentY + scrollAmount,
                                                       target.contentHeight - target.height));
            }
        }

        Item {
            id: track
            anchors.top: parent.top
            anchors.topMargin: 1
            anchors.bottom: parent.bottom
            width: parent.width

            MouseArea {
                anchors.fill: parent
                onPressed: {
                    timer.scrollAmount = target.height * (mouseY < slider.y ? -1 : 1)
                    timer.running = true;
                }
                onReleased: {
                    timer.running = false;
                }
            }

            Image {
                id:slider
                anchors.horizontalCenter: parent.horizontalCenter
                source: "qrc:/resources/buttons/slider.png"
                width: parent.width
                height: Math.min(target.height / target.contentHeight * track.height, track.height) < 20 ? 20 : Math.min(target.height / target.contentHeight * track.height, track.height)
                y: target.visibleArea.yPosition * track.height

                MouseArea {
                    anchors.fill: parent
                    drag.target: parent
                    drag.axis: Drag.YAxis
                    drag.minimumY: 0
                    drag.maximumY: track.height - height

                    onPositionChanged: {
                        if (pressedButtons == Qt.LeftButton) {
                            target.contentY = slider.y * target.contentHeight / track.height;
                        }
                    }
                }
            }
        }
    }
}

And I used scroll bar item with ListView in MyListView.qml as: 我在MyListView.qml中使用了带有ListView的滚动条项目:

MyListView.qml MyListView.qml

ListView {
    id: list
    clip: true
    anchors.margins: 5
    anchors.fill: parent
    model: 10
    delegate: trackRowDelegate
    interactive: contentHeight > height
}

ScrollBar {
    id: verticalScrollBar
    target: list
    clip: true
}

This ScrollBar item can be used with GridView as 此ScrollBar项可以与GridView一起使用

GridView {
    id: grid
    clip: true
    anchors.margins: 5
    anchors.fill: parent
    cellWidth:100
    cellHeight: 100
    model: items
    interactive: contentHeight > height
    snapMode: GridView.SnapToRow
    delegate: myDelegate
}

ScrollBar {
    id: verticalScrollBar
    target: grid
    clip: true
    visible: grid.interactive
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM