Widgets Gesture Events - How make TapAndHoldGesture look like double-click event for all children?
-
Are there any approaches at the QMainWindow level that could emulate a mouse double-click event and propagate down to the children?
I have a widget app using QStackedWidget containing multiple pages of child widgets. I was hoping to avoid having to add grabgesture code to every widget in my application. For example, the ugly experimental code below allows a TapAndHoldGesture to perform a mouse double-click and call TreeWidget:: itemActivated(). The TapAndHoldGesture works better on small screens where use find it hard to double-click on tiny TreeWidget items.
Thanks for any advice,
-Ed
@
FormBluetoothDeviceTree::FormBluetoothDeviceTree(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form),
m_localDevice(new QBluetoothLocalDevice)
{
ui->setupUi(this);
grabGesture(Qt::TapAndHoldGesture);
}bool FormBluetoothDeviceTree::event(QEvent event)
{
if (QEvent::Gesture == event->type())
{
return gestureEvent(static_cast<QGestureEvent>(event));
}
return QWidget::event(event);
}bool FormBluetoothDeviceTree::gestureEvent(QGestureEvent *event)
{
qDebug("gestureEvent():");
if (QGesture *tapAndHold = event->gesture(Qt::TapAndHoldGesture))
{
qDebug("TapAndHoldGesture");
if(tapAndHold->hasHotSpot())
{
// The hot-spot is a point in the global coordinate system,
// use QWidget::mapFromGlobal() or QGestureEvent::mapToGraphicsScene() to get a local hot-spot
QPoint hotSpotPoint = tapAndHold->hotSpot().toPoint();
QWidget target = this->childAt(this->mapFromGlobal(hotSpotPoint));
if(0 == target)
{
qDebug() << "Holding: " << "not found";
}
else
{
qDebug() << "Holding: " << target->objectName();
if("qt_scrollarea_viewport" == target->objectName())
{
qDebug() << "Holding parent: " << target->parent()->objectName();
if("treeList" == target->parent()->objectName())
{
if( 1 <= ui->treeList->selectedItems().count())
{
QTreeWidgetItem item = ui->treeList->selectedItems().at(0);
if(0 != item && Qt::GestureFinished == tapAndHold->state())
{
// act like user double-clicked item
ui->treeList->itemActivated(item, 0);
}
}
}
}
}
}
}
return true;
}
@