如何在不禁用整个控件的情况下禁用文本区域的鼠标滚轮?



我正在使用TextArea在委托中显示带有嵌入式<IMG ...>标签的多行文本,用于ListView。我将其设置为只读(而不是禁用(,因为我需要文本中的超链接可单击,因此我需要使用它onLinkActivated事件处理程序。这通常需要Label(不处理鼠标滚轮事件(,但是当文本在 HTML 中包含<IMG ...>标记时,Label不会正确呈现换行符。

我遇到的问题是,即使TextArea是只读的,也会处理鼠标滚轮事件,因此如果光标恰好位于其中一个可见的TextArea控件上,则ListView不会响应鼠标滚轮事件(因此它不会滚动(。换句话说,TextArea正在捕获鼠标滚轮事件,我希望它不要这样做。

我在文档中看到控件具有wheelEnabled:属性,但TextArea似乎不支持这一点。

更新:下面是演示该问题的最小代码示例:

import QtQuick.Controls 1.4 as Controls
Rectangle {
id: test
color: "white"
width: 300
anchors {
left: parent.left
top: parent.top
bottom: parent.bottom
}
Controls.ScrollView {
id: _scrollview
anchors.fill: parent
ListView {
anchors.fill: parent
model: 100
delegate: Rectangle {
id: tableRow
width: test.width
height: 50
color: "yellow"
TextArea {
width: test.width / 2
height: tableRow.height
readOnly: true
text: "Row # " + index
}
}
}
}
}

如果将鼠标光标悬停在此列表视图的右侧(即不在行中的TextArea控件上(,则鼠标滚轮将按预期工作。但是,如果将鼠标光标悬停在任何行中的TextArea上,则ListView将不会随鼠标滚轮滚动(因为 readOnlyTextView正在捕获事件(。

这实际上很容易,可惜我浪费了赏金。所有这些都需要一个位于TextArea上的MouseArea,如下所示:

MouseArea {
anchors.fill: txtTester
onPressed: {
mouse.accepted = false
}
onReleased: {
mouse.accepted = false
}
property int scrollValue: 15
onWheel: {
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
}
}
}

这将忽略新闻和发布事件,因此单击TextArea中的超链接仍然有效,但它会截获鼠标滚轮事件并将其应用于移动ScrollView,就好像 TextArea 不存在一样。

试试这个:

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3
Window {
visible: true
width: 400
height: 200
TextArea {
id: text
anchors.fill: parent
text: "Currentntextn\tonmovendownndownndown
ndownndownndownndownndownndownndown"
flickableItem.interactive: false
}
}

TextArea拥有flickableItem.enabled财产。由于您坚持使用!t 5.6,因此这应该对您有用。

编辑:改为flickableItem.interactive

最新更新