14.Qt中的自定义Label,事件

创建的方法

1.右键---C++----C++ class----名称myLabel,继承自Qwidget产生了2个文件

                                      

2.将mylabel.cpp和mylabel.h中的两个文件中继承QWidget修改成QLabel,因为这个是自定义的Label标签

mylabel.cpp中

MyLabel::MyLabel(QWidget *parent) : QLabel(parent)

mylabel.h中

#include <QLabel>

class MyLabel : public QLabel

3.在Ui中拉一个label标签,然后右键提升为label,这样新创建的label都是自定义的label了

                           

事件
    鼠标进入  参数要加入
    鼠标离开
    鼠标按下 
        位置信息  ev->x()  ev->y()
        判断按键  ev->button()  Qt::LeftButton
    鼠标释放
    鼠标移动
        判断按键 ev->buttons() & Qt::LeftBtton
定时器
    timerEvent
    启动 startTimer
    timerId
    第二种 QTimer
    启动 start
    timeout 信号
    stop 暂停

event
    作用  事件的分发
    返回值 bool 
        作用 true 用户自己处理 ,不向下分发
事件过滤器  上层拦截
    安装事件过滤器
    重写eventfilter

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private:
    Ui::Widget *ui;


    //定时器事件
    void timerEvent(QTimerEvent *);

    //定时器标识号
    int id1;
    int id2;

    //事件过滤器的事件
    bool eventFilter(QObject *, QEvent *);
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QTimerEvent>
#include <QTimer>
#include <QMouseEvent>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //启动定时器
    id1 = startTimer(1000); //每1000毫秒调用一次timerEvent事件
    id2 = startTimer(2000);

    //定时器第二种方式
    QTimer * timer1 = new QTimer(this);
    //启动定时器对象
    timer1->start(500); //毫秒做单位
    //每隔0.5秒发送信号
    connect(timer1,&QTimer::timeout,[=](){
        static int num = 0;
        ui->label_4->setText( QString::number(num++));
    });

    //点击按钮 暂停定时器
    connect(ui->pushButton,&QPushButton::clicked,[=](){
        timer1->stop();
    });



    //给ui->label做事件过滤器 拦截
    //步骤1  给控件安装过滤器
    // 参数this 通过父窗口给lable安装过滤器
    ui->label->installEventFilter(this);
    //步骤2  重写 eventFilter事件

}

bool Widget::eventFilter(QObject * obj, QEvent * e)
{
    if(obj == ui->label)
    {
        if( e->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent * ev =  static_cast<QMouseEvent *>(e);

            QString str = QString("事件过滤器:::鼠标按下了!!! x = %1 y = %2").arg(ev->x()).arg(ev->y());

            qDebug()  << str;
             //只有鼠标按下 自己处理 返回true
            return true;
        }
    }

    //其他让 父类处理
    return QWidget::eventFilter(obj,e);

}

void Widget::timerEvent(QTimerEvent * e)
{
    if(e->timerId() == id1)
    {
        static int num = 0;
        ui->label_2->setText( QString::number(num++));
    }

    if(e->timerId() == id2)
    {
        static int num2 = 0;
        ui->label_3->setText(QString::number(num2++));
    }

}

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

main.cpp

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

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

    return a.exec();
}

mylabel.h

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QLabel>

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


    //捕获事件
    //鼠标进入

    void enterEvent(QEvent *);

    //鼠标离开
    void leaveEvent(QEvent *);

    //鼠标按下
    void mousePressEvent(QMouseEvent *ev);
    //鼠标释放
    void mouseReleaseEvent(QMouseEvent *ev);
    //鼠标移动
    void mouseMoveEvent(QMouseEvent *ev);

    //事件分发 Event事件
    bool event(QEvent *e);

};

#endif // MYLABEL_H

mylabel.cpp

#include "mylabel.h"
#include <QDebug>
#include<QMouseEvent>
MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
    //设置鼠标追踪
   // this->setMouseTracking(true);
}


//鼠标进入

void MyLabel::enterEvent(QEvent *)
{
    //qDebug()  << "鼠标进入了!!!";

}

//鼠标离开
void MyLabel::leaveEvent(QEvent *)
{
    // qDebug()  << "鼠标离开了!!!";
}


//鼠标按下
void MyLabel::mousePressEvent(QMouseEvent *ev)
{

    //如果 鼠标按下的是左键,然后提示内容
    //找按下的 位置
//    if(ev->button() ==  Qt::LeftButton)
//    {
        QString str = QString("鼠标按下了!!! x = %1 y = %2").arg(ev->x()).arg(ev->y());

        qDebug()  << str;
//    }

}
//鼠标释放
void MyLabel::mouseReleaseEvent(QMouseEvent *ev)
{
//    if(ev->button() ==  Qt::LeftButton)
//    {
     QString str = QString("鼠标释放了!!! x = %1 y = %2").arg(ev->x()).arg(ev->y());
     qDebug()  << str;
//    }
}
//鼠标移动
void MyLabel::mouseMoveEvent(QMouseEvent *ev)
{
    //持续状态 需要用buttons  用与操作符 进行判断
//    if(ev->buttons() &  Qt::LeftButton)
//    {
     QString str = QString("鼠标移动了!!! x = %1 y = %2").arg(ev->x()).arg(ev->y());
     qDebug()  << str;
//    }
}

bool MyLabel::event(QEvent *e)
{
    //通常不会做拦截 ,event只要分发事件就可以了
   if( e->type() == QEvent::MouseButtonPress)
   {
       //static_cast父与子之间的类型转换
       QMouseEvent * ev =  static_cast<QMouseEvent *>(e);

       QString str = QString("Event:::鼠标按下了!!! x = %1 y = %2").arg(ev->x()).arg(ev->y());

       qDebug()  << str;
        //只有鼠标按下 自己处理 返回true
       return true;
   }

   //其他事件 让父亲做默认处理
   return QLabel::event(e);


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值