QT学习记录(一)事件 (event)

来源:互联网 发布:linux 进程状态日志 编辑:程序博客网 时间:2024/05/29 12:38
<pre name="code" class="cpp">
<pre name="code" class="cpp">// QT5!!/******custombutton.h************/#ifndef CUSTOMBUTTON#define CUSTOMBUTTON#include <QPushButton>#include <QMouseEvent>class CustomButton : public QPushButton{    Q_OBJECTpublic:    CustomButton(QWidget *parent = 0);protected:  void mousePressEvent(QMouseEvent *event);private:    void onButtonCliecked();};#endif // CUSTOMBUTTON/******custombutton.cpp*********/#include "custombutton.h"#include <QDebug>CustomButton::CustomButton(QWidget *parent):    QPushButton(parent){    connect(this, &CustomButton::clicked,            this, &CustomButton::onButtonCliecked);}void CustomButton::onButtonCliecked(){    qDebug() << "You clicked this!";}//构造函数中的connect(信号槽)操作将//鼠标点击这个 信号(CustomButton::clicked)(插头)//和 私有函数onButtonCliecked(插槽)连接在一起//但在重载过mousePressEvent后,上述插槽的响应不复存在//从该函数覆盖掉上面的“you clicked this”,可以推断出这个函数//本身发出了clicked()信号void CustomButton::mousePressEvent(QMouseEvent *event){    if(event->button() == Qt::LeftButton)       qDebug() << "Left";    else       QPushButton::mousePressEvent(event);}/*********main.cpp********************/#include "mainwindow.h"#include <QApplication>#include "custombutton.h"int main(int argc, char *argv[]){    QApplication a(argc, argv);    CustomButton btn;    btn.setText("This is a button");    btn.show();    return a.exec();}
收获:
1.基类的虚函数,派生类可以不重新定义,但是一旦派生类声明该函数,就必须给予实现方式,否则程序报错!
 比如该程序中的mousePressEvent继承自QPushButton。


                                             
0 0
原创粉丝点击