简体   繁体   English

Qt5 QML ListView内容缩放

[英]Qt5 QML ListView contents zoom

I am trying to implement an image viewer (sort of): 我正在尝试实现图像查看器(某种):

  • model/view approach 模型/视图方法
  • using both c++ and qml 同时使用c ++和qml
  • the images are just a ListView filled up with Image (delegate) items 图像只是一个填充了Image (代理)项的ListView

Here is a piece of code: 这是一段代码:

property double zoomFactor: 1.5;
property double imgScale: 1;

CustomToolBar
{
    id: toolBar
    onZoomInSignal:
    {
        imgScale = imgScale*zoomFactor;
    }
    onZoomOutSignal:
    {
        imgScale = imgScale/zoomFactor;

    }
    onZoomFitSignal:
    {
        imgScale = 1;
    }
}

Rectangle
{
    id: bgRect

    Layout.fillWidth: true
    Layout.fillHeight: true

    color: "Grey"

    anchors.margins: 10

    ScrollView
    {
        id: scrollView
        anchors.fill: parent

        ListView
        {
            id: listView

            anchors.fill: parent
            clip: true

            spacing: 10

            model: listItemModel

            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.left: parent.left
            anchors.right: parent.right

            anchors.margins: 10

            boundsBehavior: Flickable.StopAtBounds

            delegate: Image
            {
                id: item_id

                anchors.horizontalCenter: parent.horizontalCenter
                source: item_img_path + "/" + Math.random()

                scale: imgScale
            }
        }
    }
}

The images are loaded correctly, I need to be able to zoom them. 图像已正确加载,我需要能够缩放它们。

In order to zoom I just modify the scale property of the Image delegate. 为了缩放,我只修改Image委托的scale属性。

  • Images not scaled ( scale = 1 ) CORRECT: 图片未缩放( scale = 1 )正确:

无缩放(比例= 1)

  • Images unzoomed ( scale = 1/1.5 ) WRONG! 图像未缩放( scale = 1/1.5 )错误! images spacing (?) is being incremented: 图片间距(?)正在增加:

变焦减(比例= 1 / 1.5)

  • Images zoomed ( scale = 1.5 ) WRONG! 图像放大( scale = 1.5 )错误! images overlap: 图片重叠:

缩放加(比例= 1.5)

As you can see, zoom minus increments the spacing (I'm quite not sure it is really the spacing) between the images, and zoom plus overlaps the images. 如您所见, 缩放减会增加图像之间的间距(我不太确定它确实是间距),而缩放加会重叠图像。

Furthermore, it would be nice to have the horizontal scrollbar for the ScrollView when zooming in, but I cannot achieve this. 此外,放大时在ScrollView放置水平滚动条会很好,但是我无法实现。

Can anyone help me? 谁能帮我?

Thanks 谢谢


EDIT: 编辑:

Following the solution proposed by grillvott the images are zoomed in/out correctly, but the whole ListView is getting smaller/bigger with them: 按照Grillvott提出的解决方案,可以正确放大/缩小图像,但是整个ListView的尺寸会变得越来越小:

在此处输入图片说明

The result should be instead something like this (gimp mode ON): 结果应该是这样的(gimp模式为ON):

在此处输入图片说明

Any idea? 任何想法?

I don't think that scale will take any boundaries into consideration. 我认为scale不会考虑任何界限。 You could encapsulate the image and use fillMode to make sure the image scales accordingly: 您可以封装图像并使用fillMode来确保图像按比例缩放:

delegate: Item {
    anchors.horizontalCenter: parent.horizontalCenter
    width: img.sourceSize.width * imgScale
    height: img.sourceSize.height * imgScale
    Image {
        id: img
        anchors.fill: parent
        source: item_img_path + "/" + Math.random()
        fillMode: Image.Stretch
    }
}

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

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