Spreadsheets Example: Use TapHandler instead of MouseArea

When MouseArea is used for the header views, it blocks events for the
column/row reordering event handler.

To fix this issue, TapHandler is used instead of MouseArea. With proper
configuration of TapHandler properties, the column/row reordering event
handler can access events when they're not accepted by TapHandler.

Task-number: QTBUG-125767
Pick-to: 6.8
Change-Id: Ie59dfc681bb3d853e0a73a05d46bdcd2607091b0
Reviewed-by: Santhosh Kumar <santhosh.kumar.selvaraj@qt.io>
This commit is contained in:
MohammadHossein Qanbari 2024-07-08 15:11:59 +02:00
parent 7ed12a66f0
commit 1d344045a9
1 changed files with 80 additions and 42 deletions

View File

@ -47,6 +47,7 @@ ApplicationWindow {
clip: true
interactive: toolbar.panEnabled
syncView: tableView
selectionBehavior: HorizontalHeaderView.SelectionDisabled
selectionModel: HeaderSelectionModel {
id: horizontalHeaderSelectionModel
@ -88,41 +89,59 @@ ApplicationWindow {
}
}
function rightClicked() {
columnMenu.column = index
const menu_pos = mapToItem(horizontalHeaderView, -anchors.margins, height + anchors.margins)
columnMenu.popup(menu_pos)
}
Label {
id: horizontalTitle
anchors.centerIn: parent
text: model.columnName
}
MouseArea {
anchors.fill: parent
anchors.leftMargin: horizontalHeaderDelegate.cellPadding / 2
anchors.rightMargin: horizontalHeaderDelegate.cellPadding / 2
// handler for reset selection and context menu
TapHandler {
acceptedDevices: PointerDevice.Mouse
acceptedModifiers: Qt.NoModifier
acceptedButtons: Qt.LeftButton | Qt.RightButton
onPressed: function(event) {
if (event.modifiers === Qt.AltModifier) {
event.accepted = false
return
}
}
onClicked: function(event) {
switch (event.button) {
longPressThreshold: 0
onTapped: function(event, button) {
switch (button) {
case Qt.LeftButton:
if (event.modifiers & Qt.ControlModifier)
selectionModel.toggleColumn(index)
else
selectionModel.selectColumn(index)
selectionModel.selectColumn(index)
horizontalHeaderSelectionModel.setCurrent()
break
case Qt.RightButton:
columnMenu.column = index
const menu_pos = mapToItem(horizontalHeaderView, -anchors.margins, height + anchors.margins)
columnMenu.popup(menu_pos)
horizontalHeaderDelegate.rightClicked()
break
}
}
}
// handler for toggle selection
TapHandler {
acceptedDevices: PointerDevice.Mouse
acceptedModifiers: Qt.ControlModifier
acceptedButtons: Qt.LeftButton
longPressThreshold: 0
onTapped: function(event, button) {
selectionModel.toggleColumn(index)
horizontalHeaderSelectionModel.setCurrent()
}
}
// handler for selection and context menu in touch device
TapHandler {
acceptedDevices: PointerDevice.TouchScreen
acceptedModifiers: Qt.NoModifier
onTapped: function(eventPoint, button) {
selectionModel.toggleColumn(index)
horizontalHeaderSelectionModel.setCurrent()
}
onLongPressed: () => horizontalHeaderDelegate.rightClicked()
}
}
Menu {
id: columnMenu
@ -239,6 +258,7 @@ ApplicationWindow {
syncView: tableView
interactive: toolbar.panEnabled
movableRows: true
selectionBehavior: VerticalHeaderView.SelectionDisabled
selectionModel: HeaderSelectionModel {
id: verticalHeaderSelectionModel
@ -281,41 +301,59 @@ ApplicationWindow {
}
}
function rightClicked() {
rowMenu.row = index
const menu_pos = mapToItem(verticalHeaderView, width + anchors.margins, -anchors.margins)
rowMenu.popup(menu_pos)
}
Label {
id: verticalTitle
anchors.centerIn: parent
text: model.rowName
}
MouseArea {
anchors.fill: parent
anchors.topMargin: verticalHeaderDelegate.cellPadding / 2
anchors.bottomMargin: verticalHeaderDelegate.cellPadding / 2
// handler for reset selection and context menu
TapHandler {
acceptedDevices: PointerDevice.Mouse
acceptedModifiers: Qt.NoModifier
acceptedButtons: Qt.LeftButton | Qt.RightButton
onPressed: function(event) {
if (event.modifiers === Qt.AltModifier) {
event.accepted = false
return
}
}
onClicked: function(event) {
switch (event.button) {
longPressThreshold: 0
onTapped: function(event, button) {
switch (button) {
case Qt.LeftButton:
if (event.modifiers & Qt.ControlModifier)
selectionModel.toggleRow(index)
else
selectionModel.selectRow(index)
selectionModel.selectRow(index)
verticalHeaderSelectionModel.setCurrent()
break
case Qt.RightButton:
rowMenu.row = index
const menu_pos = mapToItem(verticalHeaderView, width + anchors.margins, -anchors.margins)
rowMenu.popup(menu_pos)
verticalHeaderDelegate.rightClicked()
break
}
}
}
// handler for toggle selection
TapHandler {
acceptedDevices: PointerDevice.Mouse
acceptedModifiers: Qt.ControlModifier
acceptedButtons: Qt.LeftButton
longPressThreshold: 0
onTapped: function(event, button) {
selectionModel.toggleRow(index)
verticalHeaderSelectionModel.setCurrent()
}
}
// handler for selection and context menu in touch device
TapHandler {
acceptedDevices: PointerDevice.TouchScreen
acceptedModifiers: Qt.NoModifier
onTapped: function(event, button) {
selectionModel.toggleRow(index)
verticalHeaderSelectionModel.setCurrent()
}
onLongPressed: () => verticalHeaderDelegate.rightClicked()
}
}
Menu {
id: rowMenu