这里以pushButton按钮的提升为例,自定义的类如下所示。
cmypushbutton.h
#ifndef CMYPUSHBUTTON_H
#define CMYPUSHBUTTON_H
#include <QPushButton>
#include <QMouseEvent>
class CMyPushButton : public QPushButton
{
public:
CMyPushButton(QWidget *parent=0);
protected:
void mousePressEvent(QMouseEvent * e);
};
#endif // CMYPUSHBUTTON_H
cmypushbutton.cpp
#include "cmypushbutton.h"
#include "QDebug"
CMyPushButton::CMyPushButton(QWidget *parent):QPushButton(parent)
{
}
void CMyPushButton::mousePressEvent(QMouseEvent * e)
{
if(e->button()==Qt::LeftButton)
{
qDebug()<<"leftbutton clicked";
}
}