我有一個MouseArea
內Scrollview
內Rectangle
。我實現了一種縮放功能,可以在按下Ctrl和滾動鼠標滾輪時放大/縮小。但是,只有當ScrollView
位於頂部時纔會放大,並且只有在ScrollView
位於底部時纔會放大。還有一些額外的邏輯來處理外部拖放文件。只要TextEdit
內的文本足夠大以獲得ScrollView
,就應該能夠複製該問題。顯然這是一個錯誤,但我無法讓它正常工作。我也試過在以下鏈接解決方案:QML MouseArea onWheel事件無法正常工作時內部QML滾動視圖
QtQuick2: Handle onWheel event inside of a ScrollView
Rectangle {
id: palGenRectangle
Layout.minimumWidth: 50
property string display
//width:800
color: "white"
ScrollView {
id: palGenTextScrollView
anchors.fill: parent
MouseArea {
id: mouseArea
anchors.fill: parent
onWheel: {
if (wheel.modifiers & Qt.ControlModifier){
if (wheel.angleDelta.y > 0)
{
mainTextEdit.font.pixelSize++
console.log("+++++")
}
else
{
mainTextEdit.font.pixelSize--
console.log("-----")
}
}
else{
wheel.accepted=true
}
}
}
DropArea {
anchors.fill: parent
onEntered: {
palGenRectangle.color = "light blue"
}
onExited: {
palGenRectangle.color = "white"
}
onDropped: {
palGenRectangle.color = "white"
if (drop.hasText) {
if (drop.proposedAction == Qt.MoveAction || drop.proposedAction == Qt.CopyAction) {
fileio.setPalFileTextFromFile(drop.text)
fileio.mainTextEdit = mainTextEdit.textDocument
drop.acceptProposedAction()
}
}
}
}
Item {
id: draggable
anchors.fill: parent
Drag.active: mouseArea.drag.active
Drag.hotSpot.x: 0
Drag.hotSpot.y: 0
Drag.mimeData: { "text/plain": palGenRectangle.display }
Drag.dragType: Drag.Automatic
Drag.onDragStarted:
Drag.onDragFinished: {
if (dropAction == Qt.MoveAction) {
item.display = ""
}
}
}
TextEdit {
id: mainTextEdit
text: fileio.palFileText
wrapMode: TextEdit.Wrap
selectByMouse: true
onTextChanged: {
if (fileio.palFileText !== mainTextEdit.text)
fileio.textIsModified = true
else
fileio.textIsModified = false
}
}
}
很好的答案,謝謝。 Math.max()獲取參數的最高值嗎?之前沒有在QML中看到它。 –
是的。 ['Math'](http://doc.qt.io/qt-5/qtqml-javascript-functionlist.html#the-math-object)是一個JavaScript內置對象。 – mcchu