Qt重写Qlabel控件,加鼠标事件

1、新建C++类

Qt重写Qlabel控件,加鼠标事件_第1张图片
Qt重写Qlabel控件,加鼠标事件_第2张图片
.h文件

#ifndef QLABELMOUSE_H
#define QLABELMOUSE_H


#include 
#include 
#include 


class QlabelMouse : public QLabel
{


    Q_OBJECT
public:
    explicit QlabelMouse(QWidget *parent = nullptr);
protected:
    void mouseReleaseEvent(QMouseEvent *ev);  //抬起
    void mousePressEvent(QMouseEvent *ev);    //按下
    void mouseDoubleClickEvent(QMouseEvent *ev);  //双击
    void mouseMoveEvent(QMouseEvent *ev);     //拖动
signals:
    void clicked(QMouseEvent *ev);  //抬起
    void pressed(QMouseEvent *ev);  //按下
    void doubled(QMouseEvent *ev);  //双击
    void moved(QMouseEvent *ev);    //拖动
public slots:




};




#endif // QLABELMOUSE_H

.cpp文件

#include "qlabelmouse.h"


QlabelMouse::QlabelMouse(QWidget *parent) : QLabel(parent)
{


}
void QlabelMouse::mouseReleaseEvent(QMouseEvent *ev)
{
    emit clicked(ev);
}
void QlabelMouse::mousePressEvent(QMouseEvent *ev)
{
    emit pressed(ev);
}
void QlabelMouse::mouseDoubleClickEvent(QMouseEvent *ev)
{
    emit doubled(ev);
}
void QlabelMouse::mouseMoveEvent(QMouseEvent *ev)
{
    emit moved(ev);
}

实验 mainwind.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"


#include 


#include 


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->label,SIGNAL(clicked(QMouseEvent* )),this,SLOT(solt11(QMouseEvent*)));
}


MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::solt11(QMouseEvent* ev)
{
    QString My_Position;
    My_Position=QString("e.pos.x:%1,e.pos.y:%2").arg(ev->pos().x()).arg(ev->pos().y());
    ui->label->setText(My_Position);
    qDebug()<<"asda";
}

mainwindow.h 文件加槽函数

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include 


namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


public slots:
    void solt11(QMouseEvent* ev);


private:
    Ui::MainWindow *ui;
};


#endif // MAINWINDOW_H

3、ui界面把Qlabel t提升为我们刚才写的类
Qt重写Qlabel控件,加鼠标事件_第3张图片

Qt重写Qlabel控件,加鼠标事件_第4张图片
4、鼠标滚轮事件

protected:
    void mouseReleaseEvent(QMouseEvent *ev);  //抬起
    void mousePressEvent(QMouseEvent *ev);    //按下
    void mouseDoubleClickEvent(QMouseEvent *ev);  //双击
    void mouseMoveEvent(QMouseEvent *ev);     //拖动
    void wheelEvent(QWheelEvent *event); //滚轮
signals:
    void emitmouseReleaseEvent(QMouseEvent *ev);  //抬起
    void emitmousePressEvent(QMouseEvent *ev);  //按下
    void emitmouseDoubleClickEvent(QMouseEvent *ev);  //双击
    void emitmouseMoveEvent(QMouseEvent *ev);    //拖动
    void emitwheelEvent(QWheelEvent *event);    //滚轮信号
void QlabelMouse::wheelEvent(QWheelEvent *event)
{
    emit emitwheelEvent(event);
}
  connect(ui->ObserveXryPicture,SIGNAL(emitwheelEvent(QWheelEvent*)),this,SLOT(slotwheelEvent(QWheelEvent*)));
void uiUser3MeasureBones::slotwheelEvent(QWheelEvent *event)
{
    //鼠标滚轮事件
    //待选区域的大小改变
    if(event->delta()>0)
    {
    //向前
          }
    else if(event->delta()<0)
    {
    //向后
          }
   
    if(event->pos().x()<My_bone.rows && event->pos().y()<My_bone.cols)
    {
           }
  
     //qDebug()<<"鼠标滚轮事件";
}

你可能感兴趣的:(Qt)