禁用QML Slider的鼠标滚轮

时间:2016-12-21 12:26:06

标签: qt qml qtquick2 qtquickcontrols

我希望能够使用鼠标滚轮(或触摸板上的两个手指)滚动Flickable,而不会更改它可能包含的Slider

示例代码和结果应用程序:

import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

ApplicationWindow {
    id: rootWindow

    visible: true
    width: 400
    height: 200
    title: qsTr("Hello World")

    ScrollView {
        anchors.fill: parent
        flickableItem.flickableDirection: Flickable.VerticalFlick

        Column {
            Repeater {
                model: 40
                Slider {
                    width: rootWindow.width * 0.9
                }
            }
        }
    }
}

result application

过去看起来有一些attempt to fix this,但没有成功。

编辑:这仅与Controls 1.x有关,因为从2.0版开始,控件似乎没有此问题。

3 个答案:

答案 0 :(得分:4)

您可以在滑块上放置MouseArea来窃取鼠标滚轮事件。

这样的事情:

import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

ApplicationWindow {
    id: rootWindow

    visible: true
    width: 400
    height: 200
    title: qsTr("Hello World")

    ScrollView {
        id: _scrollview
        anchors.fill: parent
        flickableItem.flickableDirection: Flickable.VerticalFlick

        Column {
            Repeater {
                model: 40
                Slider {
                    width: rootWindow.width * 0.9
                    property int scrollValue: 10

                    MouseArea {
                            anchors.fill: parent
                            onWheel: {
                                //check if mouse is scrolling up or down
                                if (wheel.angleDelta.y<0){
                                    //make sure not to scroll too far
                                    if (!_scrollview.flickableItem.atYEnd)
                                            _scrollview.flickableItem.contentY += scrollValue
                                }
                                else {
                                    //make sure not to scroll too far
                                    if (!_scrollview.flickableItem.atYBeginning)
                                    _scrollview.flickableItem.contentY -= scrollValue
                                }
                            }
                            onPressed: {
                                // forward mouse event
                                mouse.accepted = false
                            }
                            onReleased: {
                                // forward mouse event
                                mouse.accepted = false
                            }
                    }
                }
            }
        }
    }
}

使用onWheel - 事件将任何滚动转发到ScrollView。其他鼠标事件(例如点击)可以通过为您希望转发的任何鼠标事件设置mouse.accepted = false;来转发给父母(在本例中为滑块)。

编辑:哦,我刚才看到您不希望滑块内容发生任何变化。您还可以尝试在MouseArea上放置ScrollView并执行相同的转发。

答案 1 :(得分:4)

对您而言,最简单的方法可能是,QtQuick.Controls 1.4可以被视为deprecated,未被维护,或low-performing更改为新QtQuick.Controls 2.0 }

在此版本中,您的问题已得到解决 为了满足您对QtQuick.Controls 1.4的需求,我们将使用别名导入QtQuick.Controls 2.0

import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0 as NewCtrl

Column {
    Slider {
        id: oldslider // old slider from QtQuick.Controls 1.4 with your issue
        width: 500
        height: 250
    }

    NewCtrl.Slider {
        id: newsli // new slider without your issue. Both side by side
        width: 500
        height: 30
        wheelEnabled: false // use this to enable or disable the wheel
    }
}

当然你也可以为旧控件添加别名并使用新控件作为基本控件...或别名。如你所愿

答案 2 :(得分:-2)

来自here的黑客为我做了这件事。

Slider {
    id: slider
    Component.onCompleted: {
        for (var i = 0; i < slider.children.length; ++i) {
            if (slider.children[i].hasOwnProperty("onVerticalWheelMoved") && slider.children[i].hasOwnProperty("onHorizontalWheelMoved")) {
                slider.children[i].destroy()
            }
        }
    }
}

这可能不适用于其他控件(例如ComboBox)。