Qt鼠标事件及实例

1、效果图

在这里插入图片描述
说明:记录“左键” “中键” “右键”单击事件及显示位置; 显示双击位置坐标; 显示当前鼠标移动位置;

2、实例代码

MouseEvent.pro

#-------------------------------------------------
#
# Project created by QtCreator 2017-02-18T12:10:04
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MouseEvent
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
        mouseevent.cpp

HEADERS  += mouseevent.h

mouseevent.h

#ifndef MOUSEEVENT_H
#define MOUSEEVENT_H

#include <QMainWindow>
#include <QLabel>
#include <QStatusBar>
#include <QMouseEvent>
class MouseEvent : public QMainWindow
{
    
    
    Q_OBJECT

public:
    MouseEvent(QWidget *parent = nullptr);
    ~MouseEvent();
protected:
    void mousePressEvent(QMouseEvent *e);       //单击鼠标
    void mouseMoveEvent(QMouseEvent *e);        //移动鼠标
    //void mouseReleaseEvent(QMouseEvent *e);   //释放鼠标
    void mouseDoubleClickEvent(QMouseEvent *e); //双击鼠标
private:
    QLabel *statusLabel;
    QLabel *MousePosLabel;
};

#endif // MOUSEEVENT_H

mouseevent.cpp

#include "mouseevent.h"
#pragma execution_character_set("utf-8")

MouseEvent::MouseEvent(QWidget *parent)
    : QMainWindow(parent)
{
    
    
    setWindowTitle(tr("鼠标事件"));					//设置窗体的标题
    statusLabel = new QLabel;						//(a)
    statusLabel->setText(tr("当前位置:"));
    statusLabel->setFixedWidth(100);
    MousePosLabel = new QLabel;						//(b)
    MousePosLabel->setText(tr(""));
    MousePosLabel->setFixedWidth(100);
    statusBar()->addPermanentWidget(statusLabel);	//(c)
    statusBar()->addPermanentWidget(MousePosLabel);
    this->setMouseTracking(true);					//(d)
    resize(400,200);
}

void MouseEvent::mousePressEvent(QMouseEvent *e)
{
    
    
    QString str="("+QString::number(e->x())+","+QString::number(e->y()) +")";														//(a)
    if(e->button()==Qt::LeftButton)
    {
    
    
        statusBar()->showMessage(tr("左键: ")+str);
    }
    else if(e->button()==Qt::RightButton)
    {
    
    
        statusBar()->showMessage(tr("右键:")+str);
    }
    else if(e->button()==Qt::MidButton)
    {
    
    
        statusBar()->showMessage(tr("中键:")+str);
    }
}

void MouseEvent::mouseMoveEvent(QMouseEvent *e)
{
    
    
    MousePosLabel->setText("("+QString::number(e->x())+","+QString::number(e->y())+")");
}

//void MouseEvent::mouseReleaseEvent(QMouseEvent *e)
//{
    
    
//    QString str="("+QString::number(e->x())+","+QString::number(e->y()) +")";
//    statusBar()->showMessage(tr("释放:")+str,3000);
//}

void MouseEvent::mouseDoubleClickEvent(QMouseEvent *e)
{
    
    
    QString str="("+QString::number(e->x())+","+QString::number(e->y()) +")";
    statusBar()->showMessage(tr("双击:")+str);
}

MouseEvent::~MouseEvent()
{
    
    

}

main.cpp

#include "mouseevent.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    
    
    QApplication a(argc, argv);
    MouseEvent w;
    w.show();

    return a.exec();
}

猜你喜欢

转载自blog.csdn.net/m0_37251750/article/details/119343772