In cocos2d for OS X, I'm using ccMouseDown to detect left-clicks of the mouse and ccOtherMouseDown appears to detect clicks of the mouse wheel. I currently am unable to detect right-clicks of the mouse. I also haven't found any information about this in the API.
In cocos2d for OS X, how do I detect right-clicks of the mouse?
Not sure why this didn't occur to me to begin with, but the proper method is ccRightMouseDown. To respond with the mouse, you have to add self.isMouseEnabled = YES; in your init method.
In total, we have:
// Left click
- (BOOL) ccMouseDown:(NSEvent *)event
{
CCLOG(#"Left Mouse Button Clicked");
return YES;
}
// Right click
- (BOOL) ccRightMouseDown:(NSEvent *)event
{
CCLOG(#"Right Mouse Button Clicked");
return YES;
}
// Mouse wheel click
- (BOOL) ccOtherMouseDown:(NSEvent *)event
{
CCLOG(#"Mouse Wheel Button Clicked");
return YES;
}
Related
I am trying to implement mouse wheel zoom in/out. It works BUT when I zoom in/out, the image is getting smaller AND scrolls up/down at the same time with zoom function. Look like events exist at the same time and work together.
I cannot find how to disable scrolling with mouse wheel. May be there is a way to make possible scrolling only with mouse cursor (by clicking on scrollbar).
I was overriding the main method of mouse wheel but it was causing the effect I wrote above
void MainWindow::wheelEvent( QWheelEvent* event )
Solved by using event filter. The code below provides zoom in/out with holding ctrl button.
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::GraphicsSceneWheel)
{
ui->GV_image->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
double scaleFactor = 1.15;
bool ok = QApplication::keyboardModifiers() & Qt::ControlModifier;
if (ok)
{
QGraphicsSceneWheelEvent *scrollevent = static_cast<QGraphicsSceneWheelEvent *>(event);
if (scrollevent->delta() > 0)
{
ui->GV_image->scale(scaleFactor, scaleFactor);
}
else
{
ui->GV_image->scale(1/scaleFactor, 1/scaleFactor);
}
}
event->accept();
return true;
}
return false;
}
Put this line into your constructor or other your init function
QGraphicsView *GV_image;
...
ui->GV_image->scene()->installEventFilter(this);
I have to receive the current mouse cursor coordinates in mm/inch, while the cursor hovers over a QWidget.
Already tried: mouseMoveEvent
void darstellung::mouseMoveEvent(QMouseEvent *event){
qDebug() << event->pos();
}
I activated the MouseTracking in the MainWindow constructor as well.
setMouseTracking(true);
It seems that the mouseMoveEvent will only return the cursor position, if the left mouse button is pressed.
I have the same issue, hasMouseTracking() returns false on widget
my Workaround:
//position relative to window| |you might add some ->parentWidget()
QPoint relPos = QCursor::pos() - this->window()->pos();
the positions are relative to virtual screen-coordinates on windows. eg: my widget is displayed at the 2nd monitor, which is left to the 1st -> that gives x=-1000 y=200 (in pixel)
check capability:
if(this->hasMouseTracking() ){
this->setMouseTracking(true);
qDebug("mousetracking on");
}else qDebug("\nmousetracking not available\n");
some useless hints:
https://doc.qt.io/qt-6/qwidget.html#grabMouse (this one is no good practice)
https://stackoverflow.com/a/30960032
So in your constructor you can add a line
this->viewport()->setMouseTracking(true) and then override
mouseMoveEvent
I have a subclass of QGraphicsView that should accept two kinds of mouse events: drag and drop for scrolling and simple clicks for item selection/highlight.
So I use
setDragMode(QGraphicsView::ScrollHandDrag);
to enable scrolling the view with the "Hand". And I have a function like this:
void GraphView::mouseReleaseEvent(QMouseEvent* e)
{
if (e->button() == Qt::LeftButton)
emit leftClicked(mapToScene(e->pos()));
else
emit rightClicked(mapToScene(e->pos()));
QGraphicsView::mouseReleaseEvent(e);
}
which creates signal whenever the user clicks on the scene.
However, the problem is: when I stop dragging and release the mouse button, the mouseReleaseEvent function is called, and if the cursor happens to be over some element of the scene, it will get highlighted.
How can I changed the mouseReleaseEvent function so that the signals are created only if there was no previous drag of the mouse?
If you use mousePress and mouseMove in combination with mouseRelease, then you can determine what mouse action the user just performed.
If you have mousePress then mouseRelease, then it must be a simple click.
If you have mousePress, mouseMove, and then mouseRelease, then it must be a drag.
The Qt documentation contains an example of interpreting combinations of mouse events in action in a scribbling program.
http://doc.qt.io/qt-4.8/qt-widgets-scribble-example.html
You can extend the principle to something like this:
private bool MyWidget::dragging = false;
private bool MyWidget::pressed = false;
void MyWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
pressed=true;
}
QGraphicsView::mousePressEvent(event)
}
void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) && pressed)
{
dragging=true;
}
QGraphicsView::mouseMoveEvent(event)
}
void MyWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && pressed) {
pressed = false;
if (dragging)
{
dragging = false;
}
else
{
// plain mouse click
// do something here
}
}
QGraphicsView::mouseReleaseEvent(event)
}
Note that this does not address edge cases where a user's mouse action is performed only partially inside the widget. I must also admit that I am relatively new to Qt and have not yet used ScrollHandDrag, but this is how one would go about identifying a certain combination of mouse events.
What I am trying to implement is the following:
I have a QGraphicsitem I could use mouse or finger to press and drag on it, so its color will change gradually depend on the drag.
Also I would like to resize it using touch event.
I have already done all the mouse related event handler like mousePressEvent, mouseMoveEvent and mouseReleaseEvent. And it seems the touch event is translated into a mouse event by default under Windows.
Now I add the following code to re-implement the graphicItem's sceneEvent function:
bool MapGraphicItem::sceneEvent(QEvent * event)
{
switch (event->type()) {
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
QList<QTouchEvent::TouchPoint> touchPoints = touchEvent->touchPoints();
if (touchPoints.count() == 2) {
//do the zoom
}
return true;
}
return QGraphicsItem::sceneEvent(event);
}
The zoom also works, the problem is during pinch zoom, the mouse Events are also triggered so the color is changed which I don't want.
How to deal with this?
Right now I'm able to detect when only one button is clicked. But I want to detect when both are pressed together. The purpose is to select some items from a QTableView. I'm trying to select them in such a way that when left button is clicked on an item while right button is already kept pressed then the item will be among the selected.
The following code only shows the message that the right button is clicked, but it doesn't show that both the buttons are clicked. How can I manage to detect when both of them are clicked?
bool MainWindow::eventFilter(QObject* obj, QEvent *ev)
{
if(obj = ui->listOfImages->viewport())
{
QMouseEvent * mouseEv = static_cast<QMouseEvent*>(ev);
if(mouseEv->buttons() == Qt::RightButton)
{
qDebug()<<"Right Button clicked!";
if(mouseEv->buttons() == Qt::LeftButton)
{
qDebug()<<"Both button clicked!";
return QObject::eventFilter(obj,ev);
}
}
}
return QObject::eventFilter(obj,ev);
}
Thanks.
Try
if( (mouseEv->buttons() & Qt::RightButton) &&
(mouseEv->buttons() & Qt::LeftButton) )
{
...
}
Hint:
When you just checked buttons() is equal to Qt::RightButton, how could it be equal to Qt::LeftButton?
The QMouseEvent::buttons() function returns a combination of OR'd states of the mouse buttons.
Therefore, to test the left button is pressed, you should be doing this: -
if(mouseEv->buttons() & Qt::LeftButton)
and for the right button: -
if(mouseEv->buttons() & Qt::RightButton)
As the Qt docs state: -
For mouse press and double click events this includes the button that caused the event. For mouse release events this excludes the button that caused the event.
So you can keep track of when buttons are held down and released.