【问题标题】:QML SplitView auto collapse on handlebar mouse releaseQML SplitView 在把手鼠标释放时自动折叠
【发布时间】:2021-06-15 00:35:58
【问题描述】:

我有一个 QML Controls 2 SplitView 和一个重新定义的句柄,效果很好,但我想检测处理程序上的鼠标释放事件,所以我可以在某个宽度阈值下折叠 SplitView。在现有手柄上添加 MouseArea 将吸收拖动事件,因此我无法移动手柄。知道如何收集鼠标释放事件或解决此问题的任何其他解决方案吗?

好的,我已经创建了一个示例应用程序。正如您在此示例中所看到的,我的 MouseArea 标记为黄色,并在双击时以编程方式折叠右视图,这很好,但我还想拖动把手,并且在某个宽度阈值下释放鼠标时,我想折叠视图为好。我的 MouseArea 没有覆盖车把的车把的黑色部分响应拖动,但由于我无法从中收集到信号,因此宽度阈值已经设置了 shouldCollapse 布尔属性,因此视图不会更新。也许我可以用计时器解决这个问题,但我需要一个更复杂的解决方案。

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

Window {
    width: 800
    height: 400
    visible: true

    SplitView {
        id: splitView
        anchors.fill: parent
        orientation: Qt.Horizontal

        function toggleCollapse() { collapsibleRect.shouldCollapse = !collapsibleRect.shouldCollapse }

        handle: Rectangle {
            implicitWidth: 20
            implicitHeight: 20
            color: "black"

            MouseArea {
                anchors.centerIn: parent
                width: parent.width
                height: parent.height / 2
                onDoubleClicked: splitView.toggleCollapse()
                Rectangle {
                    anchors.fill: parent
                    color: "yellow"
                    Text {
                        anchors.centerIn: parent
                        text: "Double click to collapse"
                        rotation: 90
                    }
                }
            }
        }

        Rectangle {
            id: mainRect
            color: "green"
            SplitView.fillWidth: true
            Text {
                anchors.centerIn: parent
                font.pixelSize: 24
                text: "Main scene"
            }
        }

        Rectangle {
            id: collapsibleRect
            property bool shouldCollapse: false
            SplitView.preferredWidth: shouldCollapse ? 0 : 300
            color: "purple"
            clip: true
            onWidthChanged: {
                if(width < 200) shouldCollapse = true
                else shouldCollapse = false
            }
            Text {
                anchors.centerIn: parent
                rotation: parent.shouldCollapse ? 90 : 0
                font.pixelSize: 24
                text: parent.shouldCollapse ? "SHOULD BE COLLAPSED" : "NOT COLLAPSED"
                Behavior on rotation { NumberAnimation { duration: 100 } }
            }
        }
    }
}

【问题讨论】:

标签: qt qml


【解决方案1】:

将此添加到 MouseArea:

onPressed: {
    mouse.accepted = (mouse.flags & Qt.MouseEventCreatedDoubleClick);
}
propagateComposedEvents: true
cursorShape: Qt.SplitHCursor

【讨论】:

  • 这部分解决了问题,因为不会在 MouseArea 上触发释放事件,因为不接受按下事件。我想在假设 200 像素下折叠右侧视图。
  • 如果我理解正确,你应该把它添加到 collapsibleRect onWidthChanged if(width
  • 只要按下车把,就不会简单地以编程方式调整 SplitView 内容的大小。 SplitView 控制调整大小。最近我发现 SplitView 的“调整大小”属性在我按下 LMB 时变为 true,在我释放 LMB 时变为 false。它的名字有点误导,因为实际调整大小可能不会发生,只需按下/释放。不管怎样,谢谢你的帖子,很有帮助!
猜你喜欢
  • 2016-04-23
  • 2021-05-20
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 2019-06-01
相关资源
最近更新 更多