zoukankan      html  css  js  c++  java
  • (十)事件,定时器

    我们在QLabel控件上实现事件的demo

    建好工程之后,新建一个类

    修改继承类QWidget 为QLabel

    拉一个label控件,提升为自定义的MyLabel

    QLabel 的一些虚函数,我们可以重载实现它的一些事件

    这些函数,我们只要实现他们就好了,不需要显示地去调用,类似于回调函数

    Reimplemented Protected Functions
    
    virtual void 
    changeEvent(QEvent *ev) override
    virtual void 
    contextMenuEvent(QContextMenuEvent *ev) override
    virtual bool 
    event(QEvent *e) override
    virtual void 
    focusInEvent(QFocusEvent *ev) override
    virtual bool 
    focusNextPrevChild(bool next) override
    virtual void 
    focusOutEvent(QFocusEvent *ev) override
    virtual void 
    keyPressEvent(QKeyEvent *ev) override
    virtual void 
    mouseMoveEvent(QMouseEvent *ev) override
    virtual void 
    mousePressEvent(QMouseEvent *ev) override
    virtual void 
    mouseReleaseEvent(QMouseEvent *ev) override
    virtual void 
    paintEvent(QPaintEvent *) override
    
    4 protected functions inherited from QFrame
    35 protected functions inherited from QWidget
    9 protected functions inherited from QObject
    1 protected function inherited from QPaintDevice

    mylabel.cpp

    #include "mylabel.h"
    #include <QMouseEvent>
    #include <QTimerEvent>
    #include <QTimer>
    
    // QWidget 默认是不追踪鼠标事件的
    MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
    {
        // 设置窗口追踪鼠标键
        this->setMouseTracking(true);
    
        // 启动定时器
        // 参数 1: 触发定时器的时间, 单位: ms
        // 参数2: 使用默认值
        // 返回值: 定时器ID
        id = startTimer(2000);
        id1 = startTimer(3000);
    
    
        // 第二种定时器用法
    //    QTimer * timer = new QTimer(this);
    //    timer->start(100);
    //    connect(timer, &QTimer::timeout, this, [=]()
    //    {
    //        static int number = 0;
    //        this->setText(QString::number(number++));
    //    });
    
    }
    
    // 进入还是离开边界的一瞬间来完成的
    // 鼠标进入
    void MyLabel::enterEvent(QEvent *)
    {
        setText("你不要在我身上乱摸!!!!");
    }
    
    // 鼠标离开
    void MyLabel::leaveEvent(QEvent *)
    {
        setText("终于离开了...");
    }
    
    void MyLabel::mousePressEvent(QMouseEvent *ev)
    {
        // 字符串拼接 QString().arg()
        // %1, %2, %3 -- 占位符
        QString btn;
        if(ev->button() == Qt::LeftButton)
        {
            btn = "LeftButton";
        }
        else if(ev->button() == Qt::RightButton)
        {
            btn = "RightButton";
        }
        else if(ev->button() == Qt::MidButton)
        {
            btn = "MidButton";
        }
        QString str = QString("MousePree[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn);
    
        setText(str);
    }
    
    void MyLabel::mouseReleaseEvent(QMouseEvent *ev)
    {
        QString btn;
        if(ev->button() == Qt::LeftButton)
        {
            btn = "LeftButton";
        }
        else if(ev->button() == Qt::RightButton)
        {
            btn = "RightButton";
        }
        else if(ev->button() == Qt::MidButton)
        {
            btn = "MidButton";
        }
        QString str = QString("MouseRelease[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn);
    
        setText(str);
    }
    
    void MyLabel::mouseMoveEvent(QMouseEvent *ev)
    {
        QString btn;
        if(ev->buttons() & (Qt::LeftButton | Qt::RightButton))
        {
            btn = "LeftButton";
        }
        else if(ev->buttons() & Qt::RightButton)
        {
            btn = "RightButton";
        }
        else if(ev->buttons() & Qt::MidButton)
        {
            btn = "MidButton";
        }
        QString str = QString("MouseMove[%3]:(%1, %2)").arg(ev->x()).arg(ev->y()).arg(btn);
    
        setText(str);
    }
    
    // 每触发一次定时器, 进入该函数中
    void MyLabel::timerEvent(QTimerEvent *e)
    {
        QString str;
        if(e->timerId() == id)
        {
            static int num = -100;
            str = QString("%1: %2").arg("Time out: ").arg(num++);
            if(num >= 100)
            {
                // 关闭定时器
                killTimer(id);
            }
    
        }
        else if(e->timerId() == id1)
        {
            static int num1 = 10000;
            str = QString("%1: %2").arg("Time out: ").arg(num1++);
            if(num1 >= 10000+1000)
            {
                // 关闭定时器
                killTimer(id1);
            }
        }
    
    
        setText(str);
    }
  • 相关阅读:
    [转贴] 2016一月12日起.NET 4, 4.5 and 4.5.1 停止安全更新、技术支持 or hotfix
    Windows Azure 入门 -- VS 2015部署 ASP.NET网站(项目) 与 数据库
    [职场]工作多久才能换工作?下一个工作年薪该多高?
    [转贴] ASP.NET -- Web Service (.asmx) & JSON
    ADO.NET #3-1 (GridView + DataReader + SqlCommand)完全手写Code Behind
    File 与 Log #3--动态加入控件,[图片版]访客计数器(用.txt档案来记录)
    小图示优化
    GridView的 PreRender事件与范例--GridView + CheckBox,点选多列资料(复选删除)
    [习题] FindControl 简单练习--GridView + CheckBox,点选多列数据(复选删除)#3 List或数组
    GridView的 PreRender事件与 RowCreated、RowDataBound事件大乱斗
  • 原文地址:https://www.cnblogs.com/xiangtingshen/p/10756441.html
Copyright © 2011-2022 走看看