qml - 第二次点击后图像大小不同

标签 qml qt5 qtquick2 qtquickcontrols

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

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 中没有文本: App Start Screenshot 然后,我在 TextField 中输入一些文本,然后弹出 clear text 图标: Tesxt entered Screenshot 然后,例如,我通过单击 clear text 图标来清除文本,它(图标)再次隐藏,这没问题:
Text cleared Screenshot 最后,我将文本重新输入 TextFieldclear text 图标再次可见,但大小不同: enter image description here 为什么?我没有更改代码。一定是 Layout 有问题,但我根本看不出来!这也是 onWidthChangedonHeightChanged 处理程序的调试输出:

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

最佳答案

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。出于这个原因,这似乎是布局如何处理其项目的初始可见性的错误。我建议提交错误报告。

关于qml - 第二次点击后图像大小不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34854337/

相关文章:

c++ - 不能在 QML 中使用 C++ 类

python - PyQt5:Gtk-CRITICAL **:IA__gtk_widget_style_get:断言 'GTK_IS_WIDGET (widget)' 失败

python - Qt 带有圆角的消息

c++ - 出现对话框时如何关闭主窗口

qt - 如何正确对齐我的 qml 组件?

javascript - 迭代状态对我不起作用

qt - 为什么 QML MediaPlayer/VideoOutput 对我不起作用?

qt - 在QML中forceActiveFocus()vs focus = true

c++ - 从 C++ 加载 QML 插件

qml - 组件内部的项目访问