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

2024-02-06

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

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

Now, the purpose of this Item/Rectangle is to filter database records according to TextField's entered value, which works perfectly. However, once TextField's text is not empty anymore (when user enters some string), on the right side of Layout Image for clearing text is shown via OpacityAnimator. Once the app is launched, I get following screenshot - clear text icon is hidden since there is not text in TextField: App Start Screenshot Then, I enter some text into TextField and clear text icon pops up: Tesxt entered Screenshot Then, for instance, I clear text by clicking on clear text icon and it (icon) is hidden again, which is ok:
Text cleared Screenshot And finally, I reenter text into TextField, clear text icon is visible again, but it has different size: enter image description here Why? I did not change the code. It must be some problem with Layouts, but I simply do not see it! Here is also a debug output from onWidthChanged and onHeightChanged handlers:

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
    }
}

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

如果您阅读了文档 http://doc.qt.io/qt-5/qml-qtquick-layouts-layout.html仔细看,它并没有说有必要设置一个preferredWidth/preferredHeight如果您只想让某个项目填满可用空间。因此,布局如何处理其项目的初始可见性似乎是一个错误。我建议提交错误报告。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

第二次点击后图像大小不同 的相关文章

随机推荐

  • 确定事件之外的鼠标位置(使用 jQuery)?

    我需要使用 最好是 jQuery 来获取绝对鼠标位置 坐标 X 和 Y 如本教程 http docs jquery com Tutorials Mouse Position但在任何 JavaScript 事件之外 谢谢 不可能 但是 您可以
  • 重复的元素具有相同的展开变量

    你好 我有一些来自 vuetify 的代码 https vuetifyjs com en components cards custom actions https vuetifyjs com en components cards cus
  • C 运算符优先级,a++ && b++ || 中的逻辑运算符与一元运算符++c

    在下面的代码中 int a 1 b 2 c 3 d d a b c printf d n c 输出将为 3 我得到它或评估第一个条件 将其视为 1 然后不关心其他条件 但在 c 中 一元运算符比逻辑运算符具有更高的优先级 就像在数学中一样
  • Matplotlib 图未因数据更改而更新

    我正在使用实现图像查看器matplotlib 这个想法是对图像所做的更改 例如过滤器应用程序 将自动更新 我创建了一个图来显示初始图像 并使用添加了一个按钮pyQt更新数据 数据does改变 我检查过 但图没有 However 如果在我按下
  • Express 路由器 CRUD API。无法删除

    嗯 我正在阅读 MEAN Machine 一书并遵循其中的示例 我试图找出我的代码有什么问题 这样它就不会发出任何删除请求 GET PUT 和 POST 正常工作 我的 server js 上有这段代码 app all function r
  • 使用随机生成的数字创建临时表名称

    到目前为止我有这个代码 declare random int upper int lower int rndtb varchar 20 set lower 1 set upper 999 select random ROUND upper
  • 如何在 Silverlight 中将图像环绕圆柱体

    我正在尝试用 silverlight 将图像包裹在圆柱体周围 我在谷歌上查了很多 但没有找到任何东西 据我所知 这可以通过像素着色器来完成 但不知道如何实现 是否可以 Thanks 它不是完全包裹在圆柱体上 但您将得到一个起始想法 示例 代
  • 在.net core中对mongodb存储引用关系进行建模

    我是 mongo 的新手 在处理引用关系并在 net core 中对其进行建模时 我正在寻找一些有关最佳实践的指导 是的 这就是通常的 加入 mongodb 问题 但我还没有找到一个好的答案 为了简单起见 假设我有一个简单的 API 正在使
  • Laravel 5.3 db:seed 命令根本不起作用

    我所做的一切都是按书本进行的 安装了新的 Laravel 5 3 9 应用程序 我所有的非新应用程序都会产生相同的错误 run php artisan make auth 为新表创建迁移 php artisan make migration
  • 这是否是一个错过的优化机会

    我已经发布this https stackoverflow com a 59290242 10147399回答 代码 include
  • 如何使用反应钩子动态添加“refs”?

    所以我有一个数据数组 并且我正在使用该数据生成一个组件列表 我想在每个生成的元素上有一个引用来计算高度 我知道如何使用 Class 组件来做到这一点 但我想使用 React Hooks 来做到这一点 这是一个解释我想要做什么的示例 impo
  • 我们如何获取数据绑定以使用保存的实例状态?

    TL DR 如果与数据绑定一起使用的布局具有EditText 并且有一个绑定表达式android text 绑定表达式会覆盖保存的实例状态值 即使我们没有显式触发绑定评估 用户在配置更改之前输入的内容将被删除 我们如何解决这个问题 以便在配
  • 禁用根记录器的输出

    我在名为 logger py 的文件中有以下代码 import logging format asctime s name 30s levelname 8s message s logging basicConfig level loggi
  • Python底图:使用shadedrelief、bluemarble或etopo时出错(错误的经度格式?)

    我想绘制一张以太平洋为中心的南半球地图 并使用 python matplotlib 底图在其上绘制一些内容 一切工作正常 除非我尝试使用底图例程阴影浮雕 bluemarble 或 etopo 绘制背景图像 代码 没有我想在地图上绘制的内容
  • Visual Studio ALT 代码符号

    我正在寻找 Visual Studio 2010 中的 ALT 代码符号列表 具体来说 我正在寻找 PI 符号 我查了一下 发现应该是ALT 227或者ALT 960 我在 VS 中尝试过 但提供的符号不同 因此我在这里问这个问题 非常感谢
  • Java add() 方法约定

    所以 我通常是一名 ruby 程序员 所以我对 Java 约定的掌握充其量是不稳定的 如果我有一个类 A 并且想要定义一个方法来添加该类的两个实例 那么行为和返回类型的约定是什么 public class A public NotSureW
  • jQuery DataTables 搜索列是一个复选框

    我在用jQuery DataTables 单独列搜索 https datatables net examples api multi filter select html 在我的一张表和我的一列上包含复选框 HTML table class
  • 如何在 Identity Server 4 中配置“密钥材料”以使用 SQL、KeyVault 或任何其他系统?

    ID4 的源代码要求我们 配置密钥材料 以在生产中使用 我使用以下 Powershell 脚本创建适合 Identity Server 4 的密钥 not necessary for this question but others may
  • 将 Netlify CMS 与 Hugo 结合使用 - 通过图库创建帖子

    我对 Hugo 和 Netlify 都很陌生 所以我可能只是做错了 我已经成功构建并部署了一个带有画廊的网站 使用Hugo https hugodocs info the Galleria https galleria io 插件 并部署到
  • 第二次点击后图像大小不同

    我有以下最小的工作示例 取自我当前的项目 import QtQuick 2 5 import QtQuick Window 2 2 import QtQuick Layouts 1 1 import QtQuick Controls 1 4