MouseDrag on child widgets - qt

I am following this instructions to get the child events. My aim is to change the value of QSpinBox by dragging the mouse up/down by keeping the Ctrl key pressed (like in GIMP).
However, while this works for the window, QSpinBox ignores the drag and just selects the contents inside.
I have installed the eventfilter into all childs with:
eventFilter(QObject *obj, QEvent *event)
{
switch(event->type())
{
case QEvent::ChildAdded:
{
QChildEvent* ce = static_cast<QChildEvent*>(event);
// Install the filter to each new child object created
ce->child()->installEventFilter(this);
break;
}
case QEvent::ChildRemoved:
{
QChildEvent* ce = static_cast<QChildEvent*>(event);
// Remove the the filter from each new child object removed
ce->child()->removeEventFilter(this);
break;
}
case QEvent::MouseButtonPress:
{
QMouseEvent* me = static_cast<QMouseEvent*>(event);
qDebug() << QString::number(me->pos().x());
qDebug() << QString::number(me->pos().y());
return true;
}
case QEvent::MouseButtonRelease:
{
QMouseEvent* me = static_cast<QMouseEvent*>(event);
qDebug() << QString::number(me->pos().x());
qDebug() << QString::number(me->pos().y());
return true;
}
case QEvent::MouseMove:
{
QMouseEvent* me = static_cast<QMouseEvent*>(event);
qDebug() << QString::number(me->pos().x());
qDebug() << QString::number(me->pos().y());
return true;
}
}
return QWidget::eventFilter(obj, event);
}
and in the constructor using:
setMouseTracking(true);
installEventFilter(this);
How can I fully make QSpinBox ignore selection?

Related

Save and reproduce touch, mouse and keyboard input events

Is there elegant way to save and reproduce later all the input events (or at least touch ones) since Qt Quick 2 application's start until it closed or crashed/killed?
I can save all the input events of interest using the following approach (not full implementation):
class InputEventLogger
: public QObject
{
Q_OBJECT
public :
explicit InputEventLogger(QObject * const parent = Q_NULLPTR)
: QObject{parent}
{ ; }
protected :
bool eventFilter(QObject * watched, QEvent * event) Q_DECL_OVERRIDE
{
switch (event->type()) {
case QEvent::KeyPress :
case QEvent::KeyRelease : {
qDebug() << event->type();
const auto keyEvent = static_cast< QKeyEvent * >(event);
qDebug() << keyEvent->key() << keyEvent->text();
break;
}
case QEvent::MouseButtonPress :
case QEvent::MouseButtonRelease :
case QEvent::MouseButtonDblClick :
case QEvent::MouseMove : {
qDebug() << event->type();
const auto mouseEvent = static_cast< QMouseEvent * >(event);
qDebug() << mouseEvent->pos();
break;
}
case QEvent::TouchBegin :
case QEvent::TouchUpdate :
case QEvent::TouchEnd :
case QEvent::TouchCancel : {
qDebug() << event->type();
const auto touchEvent = static_cast< QTouchEvent * >(event);
qDebug() << touchEvent->touchPoints();
break;
}
case QEvent::GraphicsSceneMouseMove :
case QEvent::GraphicsSceneMousePress :
case QEvent::GraphicsSceneMouseRelease :
case QEvent::GraphicsSceneMouseDoubleClick : {
qDebug() << event->type();
break;
}
default : {
break;
}
}
return QObject::eventFilter(watched, event);
}
};
qApp->installEventFilter(new InputEventLogger{qApp});
QGraphicsScene accepts QEvent::GraphicsSceneMouse* events (translated from QEvent::MouseButton*/MouseMove). So, I need to translate corresponding events, before I pass them to QApplication::sendEvent().
Along with events I want to save their timestamps to reproduce them in exact manner in greater extent possible.
How to reproduce user input as realistic as possible?

Can not detect mousePress event when touch in Windows 7

Sample:
Label::Label(QLabel *parent) :
QLabel(parent)
{
this->show();
this->resize(200, 200);
}
void Label::mousePressEvent(QMouseEvent *event)
{
this->resize(300, 300);
}
Expected result: when i press label by touching, the label will become bigger.
Actual result: when i release or move my finger, it become bigger.
Its looks like the mousePressEvent is not detected.
BTW QML also have same problem for MouseArea onPressed
Thanks for answering, i tried to add this:
bool Label::event(QEvent *e)
{
if(e->type() == QEvent::TouchBegin)
qDebug() << "TouchBegin";
if(e->type() == QEvent::TouchCancel)
qDebug() << "TouchCancel";
if(e->type() == QEvent::TouchEnd)
qDebug() << "TouchEnd";
if(e->type() == QEvent::TouchUpdate)
qDebug() << "TouchUpdate";
return e->isAccepted();
}
But the TouchBegin event still not be detected when i pressed.

Can't catch TapAndHoldGesture

I grabGesture()ed one of my buttons:
buttons[0]->grabGesture(Qt::TapAndHoldGesture);
in the constructor, and declared:
bool event(QEvent *event);
in protected slots, and implemented it like this:
bool MyClass::event(QEvent *event)
{
if (event->type() == QEvent::Gesture){
QGestureEvent *gestevent = static_cast<QGestureEvent *>(event);
if (QGesture *gest = gestevent->gesture(Qt::TapAndHoldGesture)){
QTapAndHoldGesture *tapgest = static_cast<QTapAndHoldGesture *>(gestevent->gesture(Qt::TapAndHoldGesture));
cout << "grabbed a gesture event" << endl;
}
return true;
}
cout << "not a gesture event" << endl;
return QWidget::event(event);
}
and I keep getting "not a gesture event" printed to screen however I press (normal press / long press / ... )
What I'm trying to do is a long key press (from the keyboard)
It's said in the Qt Documentation:
A gesture could be a particular movement of a mouse, a touch screen
action, or a series of events from some other source. The nature of
the input, the interpretation of the gesture and the action taken are
the choice of the developer.
So I suppose also a keyboard can trigger QGesture events.
If the class handling the grap (MyClass) event is not the class where the gesture is detected on (QPushButton assuming buttons[0] is a QPushButton), then you need and event filter:
buttons[0]->grabGesture(Qt::TapAndHoldGesture);
buttons[0]->installEventFilter( myClass ); // myClass being a MyClass instance
Now, myClass object will be forwarded all events from buttons[0], this is done using QObject::eventFilter virtual function:
bool MyClass::eventFilter(QObject *obj, QEvent *event)
{
if ( event->type() == QEvent::Gesture && obj == buttons[0] )
{
QGestureEvent *gestevent = static_cast<QGestureEvent *>(event);
if (QGesture *gest = gestevent->gesture(Qt::TapAndHoldGesture)){
QTapAndHoldGesture *tapgest = static_cast<QTapAndHoldGesture *>(gestevent->gesture(Qt::TapAndHoldGesture));
cout << "grabbed a gesture event" << endl;
return true;
}
}
// standard event processing
return Parent::eventFilter(obj, event); // Parent being MyClass parent type, maybe QDialog or QWidget
}

Can I get mouse events in a QGraphicsItem?

I have subclassed QGraphicsRectItem, and it's not receiving any mouse events. I've seen other questions similar to this say I need to enable mouse tracking, but setMouseTracking is in QWidget, and QGraphicsItem does not appear to be a QWidget.
I've implemented paint, and that's working. In my subclassed QGraphicsView I am getting mouse events.
The docs seem to think I should just override the mousePressEvent function (for example) and I should start getting the events. Whether I forward the mousePressEvent to the superclass of my QGraphicsView or not doesn't seem to make any difference.
In your subclassed QGraphicsView, you need to call the default implementations of overridden mouse event methods if you want them to propagate down to the items. For example:
CustomView::mousePressEvent(QMouseEvent *event)
{
// handle the event as you like
QGraphicsView::mousePressEvent(event); // then call default implementation
}
If you want to accept hover events, you need to call QGraphicsItem::setAcceptHoverEvents(true);. Otherwise you do not need to enable any particular mouse tracking.
EDIT: Here is a full working example:
#include <QtGui>
class CustomView : public QGraphicsView
{
protected:
void mousePressEvent(QMouseEvent *event)
{
qDebug() << "Custom view clicked.";
QGraphicsView::mousePressEvent(event);
}
};
class CustomItem : public QGraphicsRectItem
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "Custom item clicked.";
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CustomItem item;
item.setRect(20, 20, 60, 60);
QGraphicsScene scene(0, 0, 100, 100);
scene.addItem(&item);
CustomView view;
view.setScene(&scene);
view.show();
return a.exec();
}
I went through the same problems you have encountered and I wanted to add some insights on top of Anthony's really good answer. Here is an example I wrote showing some features that can be implemented using the mouse events and the keyboard events.
Note that the events do not propagate to QGraphicsItems in a QGraphicsItemGroup or in a QList<QGraphicsItem> (it took me a while to figure that out).
#include <QtGui>
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include <QApplication>
#include <QGraphicsSceneMouseEvent>
class CustomItem : public QGraphicsEllipseItem
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton) {
if(event->modifiers() == Qt::ShiftModifier) {
qDebug() << "Custom item left clicked with shift key.";
// add the item to the selection
setSelected(true);
} else if(event->modifiers() == Qt::AltModifier){
qDebug() << "Custom item left clicked with alt key.";
// resize the item
double radius = boundingRect().width() / 2.0;
_center = QPointF(boundingRect().topLeft().x() + pos().x() + radius, boundingRect().topLeft().y() + pos().y() + radius);
QPointF pos = event->scenePos();
qDebug() << boundingRect() << radius << this->pos() << pos << event->pos();
double dist = sqrt(pow(_center.x()-pos.x(), 2) + pow(_center.y()-pos.y(), 2));
if(dist / radius > 0.8) {
qDebug() << dist << radius << dist / radius;
_isResizing = true;
} else {
_isResizing = false;
}
} else {
qDebug() << "Custom item left clicked.";
QGraphicsItem::mousePressEvent(event);
event->accept();
}
} else if(event->button() == Qt::RightButton) {
qDebug() << "Custom item right clicked.";
event->ignore();
}
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(event->modifiers() == Qt::AltModifier && _isResizing){
QPointF pos = event->scenePos();
double dist = sqrt(pow(_center.x()-pos.x(), 2) + pow(_center.y()-pos.y(), 2));
setRect(_center.x()-this->pos().x()-dist, _center.y()-this->pos().y()-dist, dist*2, dist*2);
} else if(event->modifiers() != Qt::AltModifier) {
qDebug() << "Custom item moved.";
QGraphicsItem::mouseMoveEvent(event);
qDebug()<<"moved"<<pos();
}
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if(event->modifiers() == Qt::AltModifier && _isResizing) {
_isResizing = false;
} else if(event->modifiers() != Qt::ShiftModifier) {
QGraphicsItem::mouseReleaseEvent(event);
}
}
int type() const
{
// Enable the use of qgraphicsitem_cast with this item.
return UserType+1;
}
private:
QPointF _center;
bool _isResizing;
};
class CustomScene : public QGraphicsScene
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "Custom scene clicked.";
QGraphicsScene::mousePressEvent(event);
if(!event->isAccepted()) {
if(event->button() == Qt::LeftButton) {
// add a custom item to the scene
QPointF pt = event->scenePos();
CustomItem * item = new CustomItem();
item->setRect(pt.x()-25, pt.y()-25, 50, 50);
item->setFlags(QGraphicsItem::ItemIsSelectable|
QGraphicsItem::ItemIsMovable);
addItem(item);
} else if(event->button() == Qt::RightButton) {
// check whether there is an item under the cursor
QGraphicsItem * itemToRemove = NULL;
foreach(auto item, items(event->scenePos())) {
if(item->type() == QGraphicsItem::UserType+1) {
itemToRemove = item;
break;
}
}
if(itemToRemove) {
// remove the item from the graphicsScene
removeItem(itemToRemove);
}
}
}
}
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "Custom scene moved.";
QGraphicsScene::mouseMoveEvent(event);
}
void keyPressEvent(QKeyEvent * event) {
if(event->key() == Qt::Key_Backspace) {
// remove all selected items
qDebug() << "selected items" << selectedItems().size();
while(!selectedItems().isEmpty()) {
removeItem(selectedItems().front());
}
} else {
QGraphicsScene::keyPressEvent(event);
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CustomItem item;
item.setRect(20, 20, 60, 60);
item.setFlags(QGraphicsItem::ItemIsSelectable|
QGraphicsItem::ItemIsMovable);
CustomScene scene;
scene.setSceneRect(0, 0, 500, 500);
scene.addItem(&item);
QGraphicsView view;
view.setScene(&scene);
view.show();
return a.exec();
}
Hope it helps too!
I had a similar problem with a view not accepting mouse clicks. The problem was that I needed to enable the view ( ui->view->setEnabled(true) ).

Why am I unable to perform a calculation for the position in a sceneEvent for a QGraphicsObject?

I have implemented QGraphicsObject with QTouchEvents and reimplemented the sceneEvent function.
bool LynxItem::sceneEvent(QEvent *event)
{
//qDebug() << "LynxItem::sceneEvent:" << itemId;
switch (event->type()) {
case QEvent::Gesture:
{
qDebug() << "LynxItem::sceneEvent:Gesture" << itemId;
return gestureEvent(static_cast<QGestureEvent*>(event));
break;
}
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
{
qDebug() << "LynxItem::sceneEvent:TouchUpdate" << itemId;
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
/* This doesn't allow two objects to move at the same time */
//setPos ( touchPoint1.scenePos() - touchPoint1.pos());
/* This does work but the item is always moved from top left */
setPos(touchPoint1.scenePos());
event->accept();
break;
}
default:
return QGraphicsItem::sceneEvent(event);;
}
return true;
}
My problem is that when I touch the item the items top right corner comes to the touch point. I want to offset the point inside where I touched. However when I do that I can only move one item at a time.
Ok, to answer my own question:
setPos ( touchPoint1.scenePos() - touchPoint1.pos());
Is incorrect. On the TouchBegin I should store touchPoint1.pos():
m_TouchOffset = touchPoint1.pos();
Then use that first position instead
setPos ( touchPoint1.scenePos() - m_TouchOffset);

Resources