新生成的控件怎么继承事件过滤器里的功能

新生成的控件如何继承事件过滤器里的功能?
这是我写的个事件过滤器 功能是能拖动QLabel   我后来新new一个QLabel  但这个QLabel不能拖动  请问应该如何继承事件过滤器里的功能呃?

bool Dialog::eventFilter(QObject *o, QEvent *evt)
{
    QWidget *l;
    l = qobject_cast<QWidget *>(o);
    static QPoint lastPnt;
    static bool isHover = false;
    if(evt->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent* e = static_cast<QMouseEvent*>(evt);
        if(l->rect().contains(e->pos()) && //如果鼠标点击
            (e->button() == Qt::LeftButton)) //如果鼠标点击的是左键
        {
            if(QString(l->metaObject()->className())=="QLabel")
            {
                sxLab();
                l->setStyleSheet("QLabel{border-width: 1px;border-style: solid;border-color: rgb(255,0,0);background:rgba(0,0,0,0%);}");
            }
            lastPnt = e->pos();
            isHover = true;
        }
    }
    else if(evt->type() == QEvent::MouseMove && isHover)
    {
        QMouseEvent* e = static_cast<QMouseEvent*>(evt);
        int dx = e->pos().x() - lastPnt.x();
        int dy=e->pos().y()-lastPnt.y();
        if(l->x()+dx<0||l->y()+dy<0||l->x()+dx>this->width()-l->width()||l->y()+dy>this->height()-l->height())
        {}
        else
        {
            l->move(l->x()+dx,l->y()+dy);
        }
    }
    else if(evt->type() == QEvent::MouseButtonRelease && isHover)
    {
        isHover = false;
    }
    return false;
}
------解决方案--------------------
就是你写个从QLabel继承下来的类,
再重写这个类的eventFilter方法:
类声明示例:
#ifndef GCLICKLABEL_H
#define GCLICKLABEL_H

#include <QLabel>
#include <QMouseEvent>

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

    void setEnterNeedUnderLine(bool need);
    void setTextColor(const QColor &color);
    void setTextHoverColor(const QColor &color);
void setHoverColor(const QColor &color);
    void setText(const QString &text);

signals:
    void clicked();
    void enterLabel();
    void leaveLabel();

protected:
    void mouseReleaseEvent(QMouseEvent * event);
    void mousePressEvent(QMouseEvent *ev);
    void enterEvent(QEvent *);
    void leaveEvent(QEvent *);
    void eventFilter(QObject *, QEvent *);

private:
    bool m_needUnderLine;
    QColor m_color;
    QColor m_hoverColor;
QColor m_widgetHoverColor;
};
------解决方案--------------------
引用:
Quote: 引用:

还是按照这个原来,重写你QLabel的eventFilter方法。
也就是从QLabel派生一个类重写它的eventFileter方法。


怎么重写呢  我是点击按钮就生成一个QLabel 希望生成的QLabel也能移动
void Dialog::on_pushButton_2_clicked()
{
    QLabel *lab=new QLabel("test Label",this);
    lab->show();
}


加一句:lab->installEventFilter(this);