Qt QML ComboBox覆盖滚轮事件 [英] Qt QML ComboBox override wheel events

查看:685
本文介绍了Qt QML ComboBox覆盖滚轮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以覆盖ComboBox MouseArea来忽略滚轮事件,而不是更改当前索引吗? ComboBox本身没有选择来更改方向盘对焦行为.到目前为止,我已经尝试使用以下代码覆盖CB MouseArea的onWheel:

ComboBox {
  Component.onCompleted: {
    for (var i = 0; i < combobox_ctrl.children.length; ++i) {
      console.log(combobox_ctrl.children[i])
      console.log(combobox_ctrl.children[i].hasOwnProperty('onWheel'))

      if (combobox_ctrl.children[i].hasOwnProperty('onWheel')) {
        console.log(combobox_ctrl.children[i]['onWheel'])
        combobox_ctrl.children[i]['onWheel'] = function() { console.log("CB on wheel!") }
        //combobox_ctrl.children[i]onWheel = function() { console.log("CB on wheel!") 
        //combobox_ctrl.children[i].destroy()
      }
    }
  }
}

但是我得到

TypeError:无法分配给只读属性"wheel"

有人能够禁用Qml中ComboBox上的转轮事件吗?

//编辑

例如在Slider控件中,我能够删除像这样的转轮事件处理:

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

但是在滑块中,WheelArea不负责处理点击"事件.

解决方案

当前不可行,因为ComboBox不是从MouseArea派生而来,而是FocusScope,它不支持此类事件./p>

最近在建议中提到了类似的问题:

在QtQuick.Controls上禁用鼠标滚轮滚动事件


如果您想这么做,似乎您剩下的唯一选择是对ComboBox.qml应用补丁程序,以删除onWheel处理程序:

diff --git a/src/controls/ComboBox.qml b/src/controls/ComboBox.qml
index 4e29dfe..3413cac 100644
--- a/src/controls/ComboBox.qml
+++ b/src/controls/ComboBox.qml
@@ -407,13 +407,6 @@ Control {
                 popup.toggleShow()
             overridePressed = false
         }
-        onWheel: {
-            if (wheel.angleDelta.y > 0) {
-                __selectPrevItem();
-            } else if (wheel.angleDelta.y < 0){
-                __selectNextItem();
-            }
-        }
     }

另一种不涉及修改Qt代码的替代方法是在中间添加MouseArea ComboBox,然后以某种方式仅将特定事件转发到ComboBox MouseArea.或者,创建一个自定义的C ++项目,该项目执行等效操作.您可以通过这种方式进行更多控制.

Is there any way to override ComboBox MouseArea to ignore wheel event instead of changing current index? ComboBox itself has no option to change wheel focus behaviour. So far I've tried to override onWheel from CB MouseArea with code like this:

ComboBox {
  Component.onCompleted: {
    for (var i = 0; i < combobox_ctrl.children.length; ++i) {
      console.log(combobox_ctrl.children[i])
      console.log(combobox_ctrl.children[i].hasOwnProperty('onWheel'))

      if (combobox_ctrl.children[i].hasOwnProperty('onWheel')) {
        console.log(combobox_ctrl.children[i]['onWheel'])
        combobox_ctrl.children[i]['onWheel'] = function() { console.log("CB on wheel!") }
        //combobox_ctrl.children[i]onWheel = function() { console.log("CB on wheel!") 
        //combobox_ctrl.children[i].destroy()
      }
    }
  }
}

But I get

TypeError: Cannot assign to read-only property "wheel"

Did anyone was able to disable wheel events on ComboBox in Qml?

// EDIT

for example in Slider control I was able to remove wheel event handling like this:

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

But in slider WheelArea is not responsible for handling "click" events.

解决方案

It's not currently possible, as ComboBox is not derived from MouseArea, but FocusScope, which has no support for these kinds of events.

A similar problem was mentioned in a suggestion recently:

Disable mouse wheel scroll event on QtQuick.Controls


If you're after a hacky way of doing it, it seems like the only option you have left is to apply a patch to ComboBox.qml that removes the onWheel handler:

diff --git a/src/controls/ComboBox.qml b/src/controls/ComboBox.qml
index 4e29dfe..3413cac 100644
--- a/src/controls/ComboBox.qml
+++ b/src/controls/ComboBox.qml
@@ -407,13 +407,6 @@ Control {
                 popup.toggleShow()
             overridePressed = false
         }
-        onWheel: {
-            if (wheel.angleDelta.y > 0) {
-                __selectPrevItem();
-            } else if (wheel.angleDelta.y < 0){
-                __selectNextItem();
-            }
-        }
     }

Another alternative that doesn't involve modifying Qt code would be to add an intermediate MouseArea above ComboBox's, and then somehow only forward specific events through to ComboBox's MouseArea. Or, create a custom C++ item that does the equivalent. You may have more control that way.

这篇关于Qt QML ComboBox覆盖滚轮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆