新建一个空的项目,在UI文件中加入pushbutton,右键点击提升类,出现:
此时的提升的类选项为无;
接下来:添加新文件->自定义一个类,继承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.c中:
#include "cmypushbutton.h"
#include "QDebug"
CMyPushButton::CMyPushButton(QWidget *parent):QPushButton(parent)
{
}
void CMyPushButton::mousePressEvent(QMouseEvent * e)
{
if(e->button()==Qt::LeftButton)
{
qDebug()<<"leftbutton clicked";
}
}
所有文件:
除了mainwindow.ui一个按钮控件,其他没有东西,进入mainwindow.ui
点击提升为->输入自定义类的名称:
点击添加:
勾选,点击提成,之后生成就可以了,现在看效果:
成功之后类名也会发生相应的变化
点击发现出现的是CMyPushButton类的打印信息,窗口却是MainWindow