Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Handling Mouse Events in Qt Applications

Tech May 12 3

Mouse events form a critical component of GUI interaction in Qt applications. Mastering mouse event handling provides a foundation for understanding other event types, as their underlying principles are large consistent.

Custom Widget Implementation

A custom label class is created to demonstrate mouse event processing:

mylabel.h

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>

class MyLabel : public QLabel
{
    Q_OBJECT
public:
    explicit MyLabel(QWidget *parent = nullptr);

protected:
    void enterEvent(QEvent *) override;
    void leaveEvent(QEvent *) override;
    void mousePressEvent(QMouseEvent *) override;
    void mouseReleaseEvent(QMouseEvent *) override;
    void mouseMoveEvent(QMouseEvent *) override;
};

#endif // MYLABEL_H

mylabel.cpp

#include "mylabel.h"
#include <QMouseEvent>

MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
    setMouseTracking(true);
}

void MyLabel::enterEvent(QEvent *)
{
    setText("Do not touch me!");
}

void MyLabel::leaveEvent(QEvent *)
{
    setText("You finally left...");
}

void MyLabel::mousePressEvent(QMouseEvent *event)
{
    QString buttonName;
    switch (event->button()) {
    case Qt::LeftButton:
        buttonName = "Left Button";
        break;
    case Qt::RightButton:
        buttonName = "Right Button";
        break;
    default:
        buttonName = "Unknown";
        break;
    }

    QString message = QString("[%1] Pressed at (%2, %3)")
                      .arg(buttonName)
                      .arg(event->x())
                      .arg(event->y());

    setText(message);
}

void MyLabel::mouseReleaseEvent(QMouseEvent *event)
{
    QString buttonName;
    switch (event->button()) {
    case Qt::LeftButton:
        buttonName = "Left Button";
        break;
    case Qt::RightButton:
        buttonName = "Right Button";
        break;
    default:
        buttonName = "Unknown";
        break;
    }

    QString message = QString("[%1] Released at (%2, %3)")
                      .arg(buttonName)
                      .arg(event->x())
                      .arg(event->y());

    setText(message);
}

void MyLabel::mouseMoveEvent(QMouseEvent *event)
{
    QString buttonName;
    if (event->buttons() & Qt::LeftButton) {
        buttonName = "Left Button";
    } else if (event->buttons() & Qt::RightButton) {
        buttonName = "Right Button";
    } else {
        buttonName = "No Button";
    }

    QString message = QString("[%1] Moved to (%2, %3)")
                      .arg(buttonName)
                      .arg(event->x())
                      .arg(event->y());

    setText(message);
}

The implementation enables tracking of mouse movements, presses, and releases within the custom widget. Mouse tracking is enabled through setMouseTracking(true), which allows the widget to receive mouse move events even when no buttons are pressed.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.