qt - QML:使用 ListView.OverlayFooter 时页脚中的按钮无响应

标签 qt listview qml

编辑:该问题似乎与 Qt 5.15 有关,但在 Qt 5.14 中不会出现!

我正在学习 QML,并制作了一个由包含 ListView 的 Window 组成的应用程序。

我正在尝试添加一个按钮,它应该位于窗口底部的固定位置(当我滚动时它不应该移动)。因此,我决定将这个Button添加到ListView的footer属性中,并设置footerPositioning:ListView.OverlayFooter

但是,这样做会使按钮完全没有响应。 如果我注释掉 footerPositioning 行,按钮将按预期工作,但位置错误。

文档指出“如果 footerPositioning 未设置为 ListView.InlineFooter,则用户无法从页脚按下并轻拂列表。在任何情况下,页脚项都可能包含提供自定义处理的项或事件处理程序鼠标或触摸输入”;对我来说,这并不是说该项目(此处为按钮)事件处理不应该工作。

以下是显示按钮无响应行为的代码示例:

调试.qml

import QtQuick 2.9
import QtQuick.Window 2.9
import QtQuick.Layouts 1.9
import QtQuick.Controls 2.9

Window {

    visible: true
    width: 260
    height: 520

    ListView {
        anchors.fill: parent
        clip: true
        spacing: 6
        model: 4

        delegate: RowLayout {
            width: parent.width
            Rectangle {
                Layout.fillWidth: true
                height: 60
                color: "#ff0000"
            }
        }

        footerPositioning: ListView.OverlayFooter // comment out this line to make the button responsive
        footer: Button {
            height: 180
            width: 90
            background: Rectangle {
                id: backbtn
                color: "gray"
            }

            onClicked: {
                backbtn.color = "#00ff00"
            }
        }
    }
}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    engine.load(QUrl(QStringLiteral("qrc:/Debug.qml")));

    if (engine.rootObjects().isEmpty()) {
        return -1;
    }

    return app.exec();
}

如何在保持 ListView.OverlayFooter 的同时使按钮响应?

最佳答案

正如 Thomenson 所指出的,此问题与错误相关。在 Qt 5.15 中,使用启用了剪辑的 ListView 和 ListView.OverlayFooter 似乎会禁用页脚中的鼠标事件处理。

从 Qt 5.15 切换到 Qt 5.14 解决了该问题。

可以在这里找到相关问题的错误跟踪器:bugreports.qt.io/browse/QTBUG-85302

关于qt - QML:使用 ListView.OverlayFooter 时页脚中的按钮无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62941182/

相关文章:

java - 在 List<Object> 中,如何保留最新条目并删除具有相同字段的先前条目?

mouse - 将鼠标事件发送到 QML 对象

c++ - 为 QProgressBar 上的图像设置着色

javascript - 如何根据时间降低 qml 中的旋转速度?

android - 如何为 ListItem 制作自定义上下文菜单(如在 Baconreader 中)?

qml - QML代码覆盖率分析

qt - QML:子属性的别名

c++ - 从 Worker 暂停/恢复 Qthread

linux - 我如何检测 Linux 中的 USB 驱动器插入?

java - 更改 ListView 中单个 View 的外观