【问题标题】:Image size different after second click on it第二次点击后图片大小不同
【发布时间】:2016-01-18 12:03:12
【问题描述】:

我有以下最小的工作示例,取自我当前的项目:

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4

Window {
    visible: true

    width: Screen.width/2
    height: Screen.height/2

    property real ueMinOpacity: 0.00
    property real ueMaxOpacity: 1.00

    Rectangle {
        anchors.fill: parent
        anchors.margins: 8

        border.color: "#4682b4"
        radius: 16

        clip: true

        gradient: Gradient {
            GradientStop {
                position: 0
                color: "#ffffff"
            }   // GradientStop

            GradientStop {
                position: 1
                color: "#303030"
            }   // GradientStop
        }   // Gradient

        Rectangle {
            anchors.fill: parent
            antialiasing: true

            border.color: "#4682b4"
            border.width: 1

            radius: 16
            clip: true

            gradient: Gradient {
                GradientStop {
                    position: 0
                    color: "#ffffff"
                }   // GradientStop

                GradientStop {
                    position: 1
                    color: "#000000"
                }   // GradientStop
            }   // Gradient

            RowLayout {
                spacing: 8
                anchors.fill: parent

                TextField {
                    id: ueProductSearchTextField

                    antialiasing: true

                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    Layout.alignment: Qt.AlignLeft|Qt.AlignVCenter
                    Layout.margins: 8

                    placeholderText: qsTr("Enter product info")
                }   // TextField

                Rectangle {
                    id: ueImageWrapper

                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    Layout.alignment: Qt.AlignRight|Qt.AlignVCenter
                    Layout.margins: 8

                    antialiasing: true

                    border.color: "#4682b4"
                    border.width: 1

                    radius: 16
                    clip: true
                    visible: ueProductSearchTextField.length > 0

                    gradient: Gradient {
                        GradientStop {
                            position: 0
                            color: "#636363"
                        }   // GradientStop

                        GradientStop {
                            position: 1
                            color: "#303030"
                        }   // GradientStop
                    }   // Gradient

                    Image {
                        anchors.fill: parent

                        source: "http://www.clipartbest.com/cliparts/9iR/gEX/9iRgEXXxT.png"

                        antialiasing: true

                        clip: true

                        smooth: true

                        fillMode: Image.PreserveAspectFit

                        horizontalAlignment: Image.AlignHCenter
                        verticalAlignment: Image.AlignVCenter

                        sourceSize.width: 96
                        sourceSize.height: 96
                    }   // Image

                    MouseArea {
                        anchors.fill: parent
                        enabled: ueImageWrapper.visible

                        onClicked: {
                            ueProductSearchTextField.text="";
                        }   // onClicked
                    }   // MouseArea


                    onWidthChanged: {
                        print("ueImageWrapper.width:"+ueImageWrapper.width);
                    }   // onWidthChanged

                    onHeightChanged: {
                        print("ueImageWrapper.height:"+ueImageWrapper.height);
                    }   // onHeightChanged
                }   // Rectangle
            }   // RowLayout
        }   // Rectangle
    }   // Rectangle
}   // Window

现在,这个Item/Rectangle的目的是根据TextField的输入值过滤数据库记录,效果很好。但是,一旦TextField 的文本不再为空(当用户输入一些字符串时),在Layout 的右侧,用于清除文本的Image 将通过OpacityAnimator 显示。启动应用程序后,我得到以下屏幕截图 - 由于TextField 中没有文本,所以隐藏了明文图标 然后,我在TextField 中输入一些文本,clear text 图标弹出: 然后,例如,我通过单击 clear text 图标来清除文本,它(图标)再次被隐藏,这没关系:
最后,我在TextField 中重新输入文本,clear text 图标再次可见,但大小不同: 为什么?我没有更改代码。 Layouts 肯定有问题,但我根本没看到!这也是来自onWidthChangedonHeightChanged 处理程序的调试输出:

qml: ueImageWrapper.width:37.56521739130435
qml: ueImageWrapper.height:480
qml: ueImageWrapper.width:132.92307692307693
qml: ueImageWrapper.width:133.83783783783784

【问题讨论】:

    标签: qml qt5 qtquick2 qtquickcontrols


    【解决方案1】:

    BaCaRoZzo 的建议有效,但我也有点不确定它为什么会这样。如果你举一个更简单的例子:

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import QtQuick.Layouts 1.0
    import QtQuick.Controls 1.0
    
    Window {
        visible: true
        width: 800
        height: 800
    
        Shortcut {
            sequence: "Ctrl+Q"
            onActivated: Qt.quit()
        }
    
        Item {
            id: boundary
            width: 400
            height: 400
            anchors.centerIn: parent
    
            RowLayout {
                anchors.fill: parent
    
                Rectangle {
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    color: "steelblue"
                }
    
                Rectangle {
                    id: rect
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    color: "salmon"
                    visible: false
                }
            }
        }
    
        Rectangle {
            anchors.fill: boundary
            color: "transparent"
            border.color: "black"
        }
    
        Button {
            text: "Toggle visibility"
            onClicked: rect.visible = !rect.visible
        }
    }
    

    第二个矩形开始是不可见的,然后通过单击按钮显示/隐藏。然而,当它开始时是不可见的,一旦显示它就永远不会得到大小。另一方面,如果它一开始是可见的,那么它会得到布局宽度的一半。

    如果你仔细阅读documentation,它并不是说如果你只是想让一个项目填满可用空间,就必须设置preferredWidth/preferredHeight。出于这个原因,布局如何处理其项目的初始可见性似乎是一个错误。我建议提交错误报告。

    【讨论】:

    • 感谢您的回答。我今天没有时间测试它。 :) 我也闻起来像虫子!但是,设置preferredWidth/preferredHeight 通常是有意义的。至少如果你不想像这种情况下那样得到完美分割的空间。希望有人可以提交错误报告。
    • 是的,确实有道理,尤其是如果它不仅仅是一个不可见的“间隔”项目。
    猜你喜欢
    • 2023-03-09
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 2013-11-11
    相关资源
    最近更新 更多