【问题标题】:How to display text and image in a scrollable list?如何在可滚动列表中显示文本和图像?
【发布时间】:2021-05-19 02:05:06
【问题描述】:

我有一个页面,我想将文本按图像分开放在可滚动列表中以测试 Qt,但如果我想这样做,我必须硬定义每个图像和文本的 Y,但我不知道如何通过获得确切的 YI 必须分配的好方法来获得它。另外,我的 ScrollView 不起作用(我无法垂直滚动)。

页面:

import QtQuick 2.15
import QtQuick.Controls 2.15

Page {
    id: page
    width: window.width
    height: window.height

    title: qsTr("Text & Images")

    ScrollView {
        width: parent.width
        height: parent.height

        ListView {

            Image {
                source: "/images/test1.png"
                anchors.horizontalCenter: parent.horizontalCenter
                height: Image.height
            }
            
            Repeater {
                model: 5

                Label {
                    text: qsTr(txts["text" + (index + 1)])
                    anchors.horizontalCenter: parent.horizontalCenter
                    y: index * 400
                }

                Image {
                    source: "/images/test" + (index + 2) + ".png"
                    anchors.horizontalCenter: parent.horizontalCenter
                    y: index * 400 + 200
                    fillMode: Image.PreserveAspectFit
                }
            }
        }
    }
}

如果我不设置 Y,所有图像都显示在 y:0 中。

有人知道像其他语言一样自动执行此操作的组件吗?

编辑:我可以动态显示图像和文本,但滚动条不起作用并弄乱了所有 ColumnLayout。

我尝试将this 改编到我的代码中。

ColumnLayout {
   anchors.fill: parent

    ListView {
        flickableDirection: Flickable.VerticalFlick
        boundsBehavior: Flickable.StopAtBounds
        Layout.fillWidth: true
        Layout.fillHeight: true
        ScrollBar.vertical: ScrollBar {}

        Image {
            source: "/images/test1.png"
        }

        Repeater {
            model: 5


            Label {
                text: qsTr(txts["text" + (index + 1)])
            }

            Image {
                source: "/images/test" + (index + 2) + ".png"
            }
        }
    }
}

【问题讨论】:

  • 改用RowLayout,设置绝对硬编码位置很奇怪。
  • Repeater 只是创建了一堆项目。您仍然需要一个定位器/布局来告诉这些项目去哪里。查看 Row/Column 或 RowLayout/ColumnLayout。
  • 实际上,您是否打算将文本/图像作为 ListView 中的代表?然后完全摆脱中继器。将您的标签/图像封装在一个项目中,并将该项目分配给 ListView 的委托属性。阅读 ListView 的文档以获取示例。
  • 您也不需要在ScrollView 中嵌套ListViewListView 本身也能够显示滚动条并允许滚动。 stackoverflow.com/questions/45650226/…
  • RowLayout 用于动态放置它们,但@iam_peter 滚动条不像您发送的链接中的响应那样工作,我将使用当前代码更新问题

标签: qt qml qt6


【解决方案1】:

我使用Rectangles 而不是Images,但这应该是您的目标,对吧?

import QtQuick 2.12
import QtQuick.Controls 2.15

Rectangle {
    width: 320
    height: 240
    color: "lightGray"

    ListView {
        id: myListView

        anchors.fill: parent

        ScrollBar.vertical: ScrollBar {
            id: control

            active: true
            interactive: true

            padding: 0
            size: 1.0
            position: 1.0

            contentItem: Rectangle {
                id: controlHandle
                implicitWidth: 10
                implicitHeight: 4
                radius: width / 2
                color: "yellow"
            }

            background: Rectangle {
                id: controlTrack
                color: "green"
            }
        }

        header: Rectangle {
            width: myListView.width
            height: 40
            color: "darkGray"

            Text {
                anchors.centerIn: parent
                text: "Header"
            }
        }

        model: 20
        delegate: Item {
            width: myListView.width
            height: childrenRect.height

            Column {
                Rectangle {
                    width: myListView.width
                    height: 60
                    color: "gray"

                    Text {
                        anchors.centerIn: parent
                        text: "This is image #" + modelData
                    }
                }

                Text { text: "Subtitle " + modelData }
            }
        }
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 2016-03-14
  • 1970-01-01
相关资源
最近更新 更多