Qt QML ComboBox 覆盖滚轮事件

标签 qt combobox qml qt5.5

有没有办法覆盖 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: Cannot assign to read-only property "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()
      }
    }
  }
}

但在 slider 中 WheelArea 不负责处理“点击”事件。

最佳答案

您可以将 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;
        }
    }
}

关于Qt QML ComboBox 覆盖滚轮事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33076960/

相关文章:

c++ - QML ListView 和段错误

c++ - 在 QTCreator 中链接到 user32.lib

java - 禁用 JavaFX ComboBox 输入

python - 将 python 对象列表传递给 qml

qt - 如何在 QT/QML 中创建共享库

windows - QTableView:如何将鼠标悬停在整行上?

html - PySide QTextEdit html 表格宽度

multithreading - 在单独的线程中填充ComboBox

javascript - jQuery ComboBox : No matter what I try, 我无法将值返回到我的选择框(无法以编程方式选择所需的选项)

c++ - 单击 BB 10 C++ 按钮