Qml 如何在ScrollView和Flow中使用MouseArea
我确实希望使用鼠标earea在ScrollView中显示流中的可单击按钮。之所以采用这种结构,是因为对象是从用户动态创建的,所以我想将它们放在一个流中以确保垂直放置,并放在一个滚动视图中以确保在对象变为多个对象时滚动Qml 如何在ScrollView和Flow中使用MouseArea,qml,qt5,mousearea,Qml,Qt5,Mousearea,我确实希望使用鼠标earea在ScrollView中显示流中的可单击按钮。之所以采用这种结构,是因为对象是从用户动态创建的,所以我想将它们放在一个流中以确保垂直放置,并放在一个滚动视图中以确保在对象变为多个对象时滚动 ScrollView { clip: true id: shiftsScrollView wheelEnabled: true //ScrollBar.vertical
ScrollView {
clip: true
id: shiftsScrollView
wheelEnabled: true
//ScrollBar.vertical.policy: ScrollBar.AlwaysOn
height: 100
width: 300
anchors.left: shiftsTitle.left
anchors.top: shiftsTitle.bottom
//spacing: 0
anchors.leftMargin: 0
anchors.topMargin: 20
Flow {
id: scrollableFlow
layoutDirection: Qt.RightToLeft
flow: Flow.TopToBottom
// Objects will be filled in here at runtime
}
}
这是在js函数中创建对象的方式:
for (let i=0; i<amountOfShifts; i++) {
var myShift = Qt.createQmlObject(
'ShiftListElement {
width: 300;
height: 25;
shiftText: "'+splittedShifts[i]+'";
shiftName: "'+shiftNames[i]+'";
}',
scrollableFlow,
"shiftElement");
shiftListObjects.push(myShift);
shiftsScrollView.contentHeight = amountOfShifts * 25
}
我找到了一些建议“防止盗窃”的答案,但在这种情况下似乎没有效果。我还试着删除流,这似乎也没有什么不同。它在常规项目中的ScrollView外部之前确实有效,这让我觉得ScrollView窃取了其中的所有鼠标事件。ScrollView可根据需要滚动,并以需要的大小显示
我错过什么了吗?谢谢你的帮助。你能把这变成一个可运行的例子吗?你越容易让我们看到问题,你就越有可能得到一个好的答案。使用z坐标
Item {
id: item1
property string shiftText: "shift";
property string shiftName: "name";
width: 200
height: 100
Text {
id: text1
text: item1.shiftText
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 12
anchors.horizontalCenterOffset: -30
anchors.horizontalCenter: parent.horizontalCenter
}
Button {
id: button
width: 20
height: 20
text: qsTr("X")
anchors.verticalCenter: text1.verticalCenter
anchors.left: text1.right
anchors.leftMargin: 20
MouseArea {
id: buttonCommitShiftMouseArea
anchors.fill: parent
preventStealing: true
onClicked:
{
console.log("I work!");
}
}
}
}