-
Mouse events
Mouse events include mouse movement, mouse button press, release, click, and double-click.
Include header file when using < qmouseevent >
The mouse movement event response is realized by redefining mouseMoveEvent(QMouseEvent *e) functione->x();e->y(); // Get the relative position of the mouse
The mouse press event response is realized by redefining the mousePressEvent(QMouseEvent *e) function
if(e->button()==Qt::LeftButton) // Qt::RightButton right click Qt::MidButton middle key { statusBar()->showMessage(tr("Left key:")+str); }
The mouse release event response is implemented by redefining the mouserelease event (qmouseevent * e) function
The mouse double click event response is realized by redefining mouseDoubleClickEvent(QMouseEvent *e) function
This - > setmousetracking (true); / / form tracking mouse. True is tracking, false is not tracking -
Keyboard events
When using, include the header file < qkeyevent >
Implemented by redefining keyPressEvent(QKeyEvent *) and keyReleaseEvent(QKeyEvent *)// QT:: a series of modifier keys defined by keyboardmodifier // Qt::ControlModifier [Ctrl] // Qt::NoModifier has no modifier key // Qt::AltModifier [Alt] key // QT:: metamodifier meta key, usually available on Linux keyboard // Qt::KeypadModifier keypad key if(event->modifiers()==Qt::ControlModifier) { if(event->key()==Qt::Key_Left) // Assembly Ctrl left key combination { } } else //Processing of not pressing the Ctrl key { if(event->key()==Qt::Key_Left) { } }
-
Event filtering
Main function, function prototypevoid QObject::installEventFilter(QObject * filterObj);// Add event filter void QObject::removeEventFilter(QObject * filterObj); // Remove event filtering bool eventFilter(QObject *watched, QEvent *event); // Event filter function. If it is filtered, it returns true; otherwise, it returns false
Example
// Add filter event label1->installEventFilter(this); label2->installEventFilter(this); label3->installEventFilter(this); // Event filter function bool EventFilter::eventFilter(QObject *watched, QEvent *event) { if(watched==label1) //First, judge the object of the current event { // Determine the type of event if(event->type()==QEvent::MouseButtonPress) { // Convert event to mouse event QMouseEvent *mouseEvent=(QMouseEvent *)event; /* The following are displayed according to the key type of the mouse */ if(mouseEvent->buttons()&Qt::LeftButton) { stateLabel->setText(tr("Left click to press the left picture")); } else if(mouseEvent->buttons()&Qt::MidButton) { stateLabel->setText(tr("Middle press the left picture")); } else if(mouseEvent->buttons()&Qt::RightButton) { stateLabel->setText(tr("Right click the left picture")); } /* Show zoomed out pictures */ QMatrix matrix; matrix.scale(1.8,1.8); QImage tmpImg=Image1.transformed(matrix); label1->setPixmap(QPixmap::fromImage(tmpImg)); } /* Handle the mouse release event and restore the size of the picture */ if(event->type()==QEvent::MouseButtonRelease) { stateLabel->setText(tr("Mouse release left picture")); label1->setPixmap(QPixmap::fromImage(Image1)); } } ...... }
QT event handling
Added by notsleepy on Mon, 23 Dec 2019 17:04:01 +0200