gpt4 book ai didi

qt - 禁用 QML Slider 的鼠标滚轮

转载 作者:行者123 更新时间:2023-12-01 20:25:11 25 4
gpt4 key购买 nike

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

示例代码和结果应用:

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版本开始,控件似乎没有此问题。

最佳答案

您可以将 MouseArea 放置在 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 {
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;,可以将其他鼠标事件(例如单击)转发到父级(在本例中为 slider )。

编辑:哦,我现在才看到您不希望对 slider 内容进行任何更改。您还可以尝试在整个 ScrollView 上放置一个 MouseArea 并执行相同的转发。

关于qt - 禁用 QML Slider 的鼠标滚轮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41262798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com