首页 > 解决方案 > QuickControls2 图像不会拉伸到父级的全宽

问题描述

我有一个简单的 QuickControls2 QML 设计,其中图像应水平拉伸以填充应用程序窗口并保持其纵横比。但是当我运行我的程序时,图像不会缩放/拉伸。

是图像小于当前窗口大小并且 QT 没有放大/增加图像大小的问题吗?是否有设置让 QT 调整图像大小以填充宽度而不考虑图像大小?

你能告诉我什么是错的,我该如何解决?

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3

ApplicationWindow {
    id: applicationWindow
    visible: true
    width: 640
    height: 480
    title: qsTr("Scroll")

    ColumnLayout {
        id: mainLayout
        anchors.fill: parent

        Image {
            id: imagePane
            Layout.fillWidth: true
            fillMode: Image.PreserveAspectFit
            source: "placeholder.jpg"
        }
    }
}

在此处输入图像描述

标签: qtqmlqtquick2

解决方案


尝试这个:

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3

ApplicationWindow {
    id: applicationWindow
    visible: true
    width: 640
    height: 480
    title: qsTr("Scroll")

    ColumnLayout {
        id: mainLayout
        anchors.fill: parent

        Image {
            id: imagePane
            Layout.preferredWidth: parent.width
            Layout.preferredHeight: parent.height
            fillMode: Image.PreserveAspectFit
            source: "placeholder.jpg"
        }
    }
}

推荐阅读