Questions tagged [qmouseevent]

The QMouseEvent class contains parameters that describe a mouse event.

The QMouseEvent class contains parameters that describe a mouse event.

Mouse events occur when a mouse button is pressed or released inside a widget, or when the mouse cursor is moved.

Mouse move events will occur only when a mouse button is pressed down, unless mouse tracking has been enabled with QWidget::setMouseTracking().

Qt automatically grabs the mouse when a mouse button is pressed inside a widget; the widget will continue to receive mouse events until the last mouse button is released.

A mouse event contains a special accept flag that indicates whether the receiver wants the event. You should call ignore() if the mouse event is not handled by your widget. A mouse event is propagated up the parent widget chain until a widget accepts it with accept(), or an event filter consumes it.

Note: If a mouse event is propagated to a widget for which Qt::WA_NoMousePropagation has been set, that mouse event will not be propagated further up the parent widget chain.

The state of the keyboard modifier keys can be found by calling the modifiers() function, inherited from QInputEvent.

The functions pos(), x(), and y() give the cursor position relative to the widget that receives the mouse event. If you move the widget as a result of the mouse event, use the global position returned by globalPos() to avoid a shaking motion.

The QWidget::setEnabled() function can be used to enable or disable mouse and keyboard events for a widget.

Reimplement the QWidget event handlers, QWidget::mousePressEvent(), QWidget::mouseReleaseEvent(), QWidget::mouseDoubleClickEvent(), and QWidget::mouseMoveEvent() to receive mouse events in your own widgets.

Reference: http://qt-project.org/doc/qt-4.8/qmouseevent.html

90 questions
18
votes
3 answers

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…
Almo
  • 15,538
  • 13
  • 67
  • 95
11
votes
1 answer

How to draw a rectangle and adjust its shape by drag and drop in PyQt5

I'm trying to draw a rectangle on GUI created by PyQt5 by drag and drop. I managed to do that, but the rectangle is drawn when the mouse left key is released. What I want to do is like this link: When the mouse left button is pressed, start…
pothny3
  • 138
  • 1
  • 2
  • 7
9
votes
2 answers

Showing a popup menu on QGraphicsScene click or right click

Is there a way to show a popup when the user right clicks on an empty portion of the scene? I'm new at Qt and I've tried slots and subclassing, but to no avail. No such slot and, respectively: "error: 'QMouseEvent' has not been declared" when…
TudorT
  • 441
  • 1
  • 6
  • 18
8
votes
2 answers

How to get the released button inside MouseReleaseEvent in Qt

In MouseReleaseEvent(QMouseEvent *e), is there a way to know which button was released without using a new variable ? I mean something like in the MousePressEvent(QMouseEvent *e) with e.buttons(). I tried e.buttons() in the releaseEvent it's not…
Othman Benchekroun
  • 1,998
  • 2
  • 17
  • 36
5
votes
1 answer

How to simulate a drag and drop action using QTest

In order to create a test case for a drag and drop bug in the QTreeView widget I tried to simulate a drag and drop mouse movement behavior. I basically select the first element in the QTreeView and want it to drag and drop on the third element. I…
Aleph0
  • 5,816
  • 4
  • 29
  • 80
4
votes
1 answer

How to capture mousePressEvent between widgets in a layout?

I'm trying to detect mouse clicks for anywhere inside an area with several widgets. For this I'm using the following code: custom_widget =…
sunyata
  • 1,843
  • 5
  • 27
  • 41
4
votes
2 answers

Qt mouseReleaseEvent() not trigggered?

I got a library to display pictures, lets call it PictureGLWidget, with: class PictureGLWidget: public QGLWidget { so PictureGLWidget extends QGLWidget. In PictureGlWidget the void PictureGlWidget::mouseReleaseEvent(QMouseEvent*…
Ralf Wickum
  • 2,850
  • 9
  • 55
  • 103
3
votes
1 answer

Simulation mouse movement using QTest

I'm using QTest to test simple widgets and it all works as expected. But now I have a more complex test scenario, where I have basically a widget that allows the user to press the mouse button, then moves some content, and then releases the mouse…
Aleph0
  • 5,816
  • 4
  • 29
  • 80
3
votes
3 answers

How to move the whole window when mouse is on the window's custom widget in Qt?

Let's say I have a custom widget and add it to the main window in qt. As you can see, the red area is the custom widget. What I want to do is when the mouse is pressed in the red area and moved, the whole window will move as well. I know how to…
Theodore Tang
  • 831
  • 2
  • 21
  • 42
3
votes
1 answer

How to connect to mousePressEvent slot Qt

In Class Buttons, I have a btnRightClicked signal and a mousePressEvent slot: void Buttons::mousePressEvent(QMouseEvent *e) { if(e->button() == Qt::RightButton) { emit btnRightClicked(); } } And in mainwindow.cpp, I connect the…
Theodore Tang
  • 831
  • 2
  • 21
  • 42
3
votes
3 answers

qt get mouse clicked position relative to image in a graphics view

I want to allow user to draw lines on image using the mouse, I'm using graphics view and mouse event but the position I get is not the right position relative to the image here is the mouse event function void…
Ammar Hussein
  • 5,534
  • 5
  • 18
  • 37
3
votes
1 answer

Ignore and propagate QWidget click events to the OS

I have a top-level parentless QWidget on top of which I have a bunch of other QWidgets(e.g. QPushButtons). I would like to make my QWidget act like it is transparent for mouse events. So when I click on the QWidget(but not on my buttons) I want my…
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
2
votes
1 answer

Catch the mouse event

Don't know exactly how to give the parameters of QMouseEvent class. Should I create new class to implement a QMouseEvent into my QTextEdit? class Test(QMainWindow): def __init__(self): super().__init__() self.txt =…
2
votes
1 answer

QWidget how to detect mouse stop moving

I can do something when mouse is moving by overwriting QWidget's mouseMoveEvent function. But I want to do something at the moment when mouse stop moving. How can I implement this?
2
votes
1 answer

Implementing mouseMoveEvent for qgraphicsObject to follow cursor

I have a Qt application with objects derived from QGraphicsObjcet that need to be movable in a scene. I know that I can use the flags for movement to achieve this. myObject->setFlag(QGraphicsItem::ItemIsMovable); …
Blink
  • 163
  • 1
  • 6
1
2 3 4 5 6