我是靠谱客的博主 负责外套,这篇文章主要介绍QT 给控件添加事件过滤,现在分享给大家,希望可以做个参考。

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDebug>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

    bool eventFilter(QObject *obj, QEvent *e);

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->textEdit->setAttribute(Qt::WA_Hover,true);//开启悬停事件
    ui->textEdit->installEventFilter(this);
}

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

bool Widget::eventFilter(QObject *obj, QEvent *e)
{
    if(obj == ui->textEdit)
    {
        //if(e->type() == QEvent::Wheel)
        if(e->type() == QEvent::HoverEnter)
        {
            qDebug() << "helloworld!";
            qDebug() << number++;



        }



    }


}

最后

以上就是负责外套最近收集整理的关于QT 给控件添加事件过滤的全部内容,更多相关QT内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(109)

评论列表共有 0 条评论

立即
投稿
返回
顶部