3

有没有办法覆盖 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 {
  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 不负责处理“点击”事件。

4

5 回答 5

4

您可以放置MouseArea​​和ComboBox钢轮事件。

ComboBox {
    anchors.centerIn: parent
    model: [ "Banana", "Apple", "Coconut" ]
    MouseArea {
        anchors.fill: parent
        onWheel: {
            // do nothing
        }
        onPressed: {
            // propogate to ComboBox
            mouse.accepted = false;
        }
        onReleased: {
            // propogate to ComboBox
            mouse.accepted = false;
        }
    }
}
于 2015-10-12T11:37:36.130 回答
2

目前不可能,因为ComboBox它不是从MouseArea, 但是派生的FocusScope,它不支持这些类型的事件。

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

在 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 above ComboBox,然后以某种方式仅将特定事件转发到ComboBox's MouseArea。或者,创建一个具有相同功能的自定义 C++ 项。这样你可能会有更多的控制权。

于 2015-10-12T09:28:47.537 回答
1

好的。在四处乱窜后,我设法提出了我可以接受的解决方案,但在某些情况下可能会引入一些回归。按下和悬停的属性不再可用

import QtQuick.Controls.Private 1.0

ComboBox {
  Component.onCompleted: {
    for (var i = 0; i < combobox_ctrl.children.length; ++i) {
      if (combobox_ctrl.children[i].hasOwnProperty('onWheel') && combobox_ctrl.children[i] !== mouseArea) {
        combobox_ctrl.children[i].destroy()
      }
    }
  }

  MouseArea {
    id: mouseArea
    anchors.fill: parent

    onPressed: {
      if (combobox_ctrl.activeFocusOnPress)
        forceActiveFocus()
      if (!Settings.hasTouchScreen)
        combobox_ctrl.__popup.toggleShow()
    }

    onClicked: {
      if (Settings.hasTouchScreen)
        combobox_ctrl.__popup.toggleShow()
    }
  }
}

通过这种方式,我们可以模拟原来在 ComboBox 内的鼠标区域。弹出窗口按原样显示(至少我还没有看到任何回归)。但是现在有两个属性无法访问

于 2015-10-12T11:40:11.983 回答
1

NonScrollingComboBox.qml我在这篇文章之后创建了一个使用以下代码调用的单独文件: https ://stackoverflow.com/a/33080217/969016

现在我可以将NonScrollingComboBox其用作组件,而不是ComboBox在我不希望鼠标滚动更改值的地方

import QtQuick 2.0
import QtQuick.Controls 1.4

ComboBox {
    id: combobox_ctrl
    Component.onCompleted: {
        for (var i = 0; i < combobox_ctrl.children.length; ++i) {
            if (combobox_ctrl.children[i].hasOwnProperty('onWheel')
                    && combobox_ctrl.children[i] !== mouseArea) {
                combobox_ctrl.children[i].destroy()
            }
        }
    }
    MouseArea {
        id: mouseArea
        anchors.fill: parent

        onPressed: {
            if (combobox_ctrl.activeFocusOnPress)
                forceActiveFocus()
            combobox_ctrl.__popup.toggleShow()
        }

        onClicked: {
            combobox_ctrl.__popup.toggleShow()
        }
    }
}

用法:

NonScrollingComboBox {
    anchors.verticalCenter: parent.verticalCenter
    model: ["item one", "item 2"]
}
于 2017-10-14T08:37:40.763 回答
0

这似乎只适用于Qt Quick Controls 1 ComboBox。在Qt Quick Controls 2 ComboBox 上,滚轮鼠标事件默认不启用,可以通过将属性设置为 true 手动启用wheelEnabled(记录在基类Control中)。此外,组合框不会在鼠标事件上保持“焦点”,因此您只需输入它们即可在其他鼠标区域自由使用滚轮。

于 2017-06-17T11:27:16.213 回答