【问题标题】:QML window is bouncing while changing size and positionQML 窗口在更改大小和位置时弹跳
【发布时间】:2022-01-11 15:49:17
【问题描述】:

我的目的是在调整大小时将窗口粘贴到屏幕的右对齐。 在窗口宽度和 x(绑定到宽度)的动画(或更改显式)期间,ApplicationWindow 向左弹跳(显式更改闪烁)。

我已经在寻找解决方案,但没有什么对我有用。 添加属性:

QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);

帮了很多忙,但窗口仍然左右弹跳。

main.qml:

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15

ApplicationWindow {
    id: root
    height: 300
    width: 300
    visible: true
    flags: Qt.FramelessWindowHint
    x: Screen.width - width

    Material.theme: Material.Dark
    
    NumberAnimation on width {
        id: animation
        easing.type: Easing.InOutQuad
    }

    property var lowerSize: true

    Rectangle {
        anchors.fill: parent
        
        Button { 
            anchors.centerIn: parent
            
            enabled: !animation.running
            text: lowerSize ? 'Size up' : 'Size down'
            onClicked: { 
                animation.from = lowerSize ? 300 : 600
                animation.to = lowerSize ? 600 : 300
                animation.start()
                lowerSize = !lowerSize
            }
        }
    }
}

我已经在 github qml-flickering-example 上制作了用于重现问题的示例代码。

【问题讨论】:

    标签: qt qml window


    【解决方案1】:

    我在 macOS 上的行为与您在 Windows 中看到的略有不同(对我来说收缩时口吃),但它确实仍然口吃。

    这似乎是因为您正在为宽度设置动画,然后依赖 QML 绑定来更新 x。因此,x 更新比宽度更新滞后一帧。

    使用 ParallelAnimation 并同时为其设置动画为我修复了它:

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.Controls 2.15
    import QtQuick.Controls.Material 2.15
    
    ApplicationWindow {
        id: root
        height: 300
        width: 300
        visible: true
        flags: Qt.FramelessWindowHint
    
        Component.onCompleted: {
            x = Screen.width - width;
            y = 0;
        }
    
        Material.theme: Material.Dark
    
        ParallelAnimation {
            id: animation
    
            NumberAnimation {
                id: widthAnimation
                target: root
                property: "width"
                easing.type: Easing.InOutQuad
            }
            NumberAnimation {
                id: xAnimation
                target: root
                property: "x"
                easing.type: Easing.InOutQuad
            }
        }
    
        property bool lowerSize: true
    
        Rectangle {
            anchors.fill: parent
            
            Button { 
                anchors.centerIn: parent
                
                enabled: !animation.running
                text: lowerSize ? 'Size up' : 'Size down'
                onClicked: { 
                    widthAnimation.from = lowerSize ? 300 : 600
                    widthAnimation.to = lowerSize ? 600 : 300
    
                    xAnimation.from = lowerSize ? Screen.width - 300 : Screen.width - 600
                    xAnimation.to = lowerSize ? Screen.width - 600 : Screen.width - 300
    
                    animation.start()
                    lowerSize = !lowerSize
                }
            }
        }
    }
    

    【讨论】:

    • 对我没有帮助。 Windows 10 上的相同行为
    • 我唯一的猜测是,由于某种原因,窗口管理器正在延迟与调整大小相关的窗口重新定位(不管它们是否同时设置)。可能是 Windows 正在为与您的动画冲突的 x 或宽度变化设置动画。您是否尝试过仅在没有动画的情况下更改 x 或宽度,并查看 Windows 是否正在执行自己的更改动画?
    • 顺便说一句,我注意到您的 CMake 设置在每次运行时都没有正确重新编译 QML。您可能想尝试清理/重建,看看这是否确实是一个问题。
    • 是的,我尝试只更改 x 和宽度,看起来不错。问题仅出现在并行动画中。0 QML 文件没有添加到 qml.qrc 以有意识地进行编译以更快地实现。编译 cpp 源代码后,我从 working_dir=${CMAKE_SOURCE_DIR} 运行 exe,启动后重新加载 qml 文件,无需重新编译
    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多