QML:ScrollView和MouseAreas:在滚动条

时间:2017-07-16 21:11:28

标签: hover qml scrollview scrollbar mousearea

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

为此,当鼠标悬停在我的一个MouseAreas上时,我使用MouseAreas并进行控制。我解决了一些问题(参见QML: Problems with mousearea overlapping),但现在我发现了ScrollView的另一个问题(该对话框必须包含带有ListView的ScrollView)。当鼠标悬停在滚动条上时我无法控制...并且对话框已关闭,因为没有MouseArea“鼠标悬停...”

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

提前致谢, 迭

Test.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;
    }
}

TestMenu.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" }
    }
}

0 个答案:

没有答案