2

我有一个 QML 应用程序,当我按下按钮时需要打开一个对话框。当鼠标离开对话框时必须关闭对话框(没有点击,所以我认为'Popup'控件不能使用)。

为此,我使用 MouseAreas 并控制鼠标何时位于我的一个 MouseAreas 上。我解决了一些问题(请参阅QML:鼠标区域重叠问题),但现在我发现了 ScrollView 的另一个问题(对话框必须包含带有 ListView 的 ScrollView)。我无法控制鼠标何时悬停在滚动条上...并且对话框已关闭,因为没有 MouseArea “有”鼠标悬停...

如何控制滚动条上的鼠标事件?

在此先感谢,迭戈

测试.qml

import QtQuick 2.5
import QtQuick.Controls 2.1
import QtQuick.Window 2.0

Item {
    width: 800
    height: 800

    property bool m_bButtonHovered: false
    onM_bButtonHoveredChanged: updateOpen()

    Rectangle{
        id: rect
        anchors { top: parent.top; topMargin: 100; horizontalCenter: parent.horizontalCenter }
        height: 50; width: 50
        color: "red"

        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onEntered: m_bButtonHovered = true;
            onExited: m_bButtonHovered = false;
        }
    }

    Loader {
        id: loader_dialog
        anchors { top: rect.bottom; horizontalCenter: rect.horizontalCenter}
        active: false
        sourceComponent: TestMenu {
            onClose: loader_dialog.active = false;
        }
    }

    Timer {
        id: timerOpen
        interval: 200
        running: false
        repeat: false
        onTriggered: checkOpen();
    }

    function updateOpen() {
        if (m_bButtonHovered)
            timerOpen.start();
    }

    function checkOpen(){
        if (m_bButtonHovered)
            loader_dialog.active = true;
    }
}

测试菜单.qml

import QtQuick 2.0
import QtQuick.Controls 1.3

Rectangle {
    id: id_dialog

    signal close()

    width: 400
    height: 600

    color: "lightgrey"

    property int m_iNumHovered: 0
    onM_iNumHoveredChanged: update();

    function update() {
        if (m_iNumHovered == 0)
            timer.start();
    }

    function check() {
        if (m_iNumHovered == 0)
            id_dialog.close();
    }

    Timer {
        id: timer
        interval: 100
        running: false
        repeat: false
        onTriggered: check();
    }


    MouseArea {
        id: mouseAreaTopZone
        anchors { bottom: parent.top; horizontalCenter: parent.horizontalCenter}
        width: 50; height: 50

        hoverEnabled: true
        onEntered: m_iNumHovered++;
        onExited: m_iNumHovered--;
    }

    MouseArea {
        id: mouseAreaDialog
        anchors.fill: parent

        hoverEnabled: true
        onEntered: m_iNumHovered++;
        onExited: m_iNumHovered--;
    }

    ScrollView {
        id: id_scrollView
        anchors.centerIn: parent
        height: 400; width: parent.width

        ListView {
            id: id_list
            model: modelTest
            anchors { left: parent.left; right: parent.right }
            height: contentHeight

            clip: true
            interactive: false
            delegate: Text {
                    height: 100
                    width: id_list.width
                    text: displayname
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
             }
        }
    }

    ListModel {
        id: modelTest
        ListElement { optionId: 1; displayname: "Option 1" }
        ListElement { optionId: 2; displayname: "Option 2" }
        ListElement { optionId: 3; displayname: "Option 3" }
        ListElement { optionId: 4; displayname: "Option 4" }
        ListElement { optionId: 5; displayname: "Option 5" }
        ListElement { optionId: 6; displayname: "Option 6" }
        ListElement { optionId: 7; displayname: "Option 7" }
    }
}
4

0 回答 0