1.自定义的控件封装
1.首先添加一个 QT设计师界面类
2. 设计新添加的设计类
3.在原来的ui 设计界面中添加一个 Widget 图形框
然后 右键,提升为 进入界面
4.写代码:来实现自定义的控件封装
- //QSpinBox 移动, Qslider 跟着移动
- //控件的范围0 -- 99
- void (QSpinBox:: *spSignal) (int )=& QSpinBox::valueChanged;// 函数指针 指向有参函数的函数,参数类型为 int
- connect(ui->spinBox,spSignal,ui->horizontalSlider,&QSlider::setValue);//左边的控件参数 来连接右边的控件
-
- //QSlider 移动 QSpinBox 跟着移动
- connect(ui->horizontalSlider,& QSlider::valueChanged,ui->spinBox,&QSpinBox::setValue);
5.定时器的使用方法,
第一种在 .h 文件里面定义
void timerEvent(QTimerEvent *ev); //函数重写 int id1;//定时器1 //标记定时器是哪个 int id2;//定时器2
.cpp 文件实现
- /
- //构造函数里面
-
-
- //启动定时器 的第一种方法
- id1= startTimer(1000);//参数1 时间间隔 单位 毫秒 一秒加一
- id2=startTimer(2000);//两秒的加1
-
- //
-
-
- //定时器1 的使用方法
- void Widget::timerEvent(QTimerEvent *ev)
- {
- if(ev->timerId()==id1)
- {
- static int num=1;
- ui->label_time1->setText(QString::number(num++));//把数字转化为字符串
- }
- if(ev->timerId()==id2)
- {
- static int num2=1;
- ui->label_time2->setText(QString::number(num2++));//把数字转化为字符串
- }
-
-
- }
6.第二种定时器的使用方法
头文件:#include <QTimer>//第二种定时器的头文件
代码: (写在构造函数里面)
-
- //启动定时器的第二种方法
- QTimer * timer1 =new QTimer(this);
- //启动定时器
- timer1->start(500);//参数单位 毫秒
- connect(timer1,&QTimer::timeout,[=](){
- static int num3=1;
- ui->label_time3->setText(QString::number(num3++));
-
- });
7. 事件分发器,和事件过滤器的定义
8.我的ui -> 设计界面的图片
9.所有的代码,
mylabel.h
- #ifndef MYLABEL_H
- #define MYLABEL_H
-
- #include
- #include
- #include
- #include "widget.h"
-
-
- class mylabel : public QLabel //这里继承的类型需要改 //他的基类是这个
- {
- Q_OBJECT
- public:
- explicit mylabel(QWidget *parent = 0);
-
- //鼠标进入事件
- void enterEvent(QEvent * event);
-
- //鼠标离开事件
- void leaveEvent(QEvent * event);
- //鼠标按下
-
- void mousePressEvent(QMouseEvent *ev);
- //鼠标释放
- void mouseReleaseEvent(QMouseEvent *ev);
- //鼠标移动
- void mouseMoveEvent(QMouseEvent *ev);
-
- //事件分发 Event事件
- bool event(QEvent *e);
-
-
- signals:
-
- public slots:
- };
-
- #endif // MYLABEL_H
smallwidget.h
- #ifndef SMALLWIDGET_H
- #define SMALLWIDGET_H
-
- #include
- #include
- #include "widget.h"
- namespace Ui {
- class smallWidget;
- }
-
- class smallWidget : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit smallWidget(QWidget *parent = 0);
- ~smallWidget();
- void setNum(int num);//设置值
- int getNum();//获取值
-
-
-
- private:
- Ui::smallWidget *ui;
- };
-
- #endif // SMALLWIDGET_H
widget.h
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
-
- namespace Ui {
- class Widget;
- }
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- explicit Widget(QWidget *parent = 0);
- ~Widget();
-
- //定时器1
- void timerEvent(QTimerEvent *ev);
- int id1;//定时器1
- int id2;//定时器2
- //重写事件过滤器的事件
- bool eventFilter(QObject *obj,QEvent *e);
-
-
- private:
- Ui::Widget *ui;
- };
-
- #endif // WIDGET_H
main.cpp
- #include "widget.h"
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
-
- return a.exec();
- }
mylabel.cpp
- #include "mylabel.h"
- #include "widget.h"
- #include <QDebug>
- #include <QLabel>
- #include <QEnterEvent>
- #include <QObject>
-
- mylabel::mylabel(QWidget *parent) : QLabel(parent)
- {
-
- }
-
- //鼠标进入事件
- void mylabel::enterEvent(QEvent * event)
- {
-
- //qDebug()<<"鼠标进入按钮 1 里面了";
-
- }
-
- //鼠标离开事件
- void mylabel::leaveEvent(QEvent * event)
- {
- //qDebug()<<"鼠标离开了按钮 1 ";
-
- }
-
- //鼠标按下
- void mylabel::mousePressEvent(QMouseEvent *ev)
- {
-
- //如果 鼠标按下的是左键,然后提示内容
- //找按下的 位置
- // if(ev->button() == Qt::LeftButton)
- // {
- QString str = QString("鼠标按下了!!! x = %1 y = %2").arg(ev->x()).arg(ev->y());
-
- qDebug() << str;
- // }
-
- }
- //鼠标释放
- void mylabel::mouseReleaseEvent(QMouseEvent *ev)
- {
- // if(ev->button() == Qt::LeftButton)
- // {
- QString str = QString("鼠标释放了!!! x = %1 y = %2").arg(ev->x()).arg(ev->y());
- qDebug() << str;
- // }
- }
- //鼠标移动
- void mylabel::mouseMoveEvent(QMouseEvent *ev)
- {
- //持续状态 需要用buttons 用与操作符 进行判断
- // if(ev->buttons() & Qt::LeftButton)
- // {
- QString str = QString("鼠标移动了!!! x = %1 y = %2").arg(ev->x()).arg(ev->y());
- qDebug() << str;
- // }
- }
-
- bool mylabel::event(QEvent *e)
- {
- //通常不会做拦截 ,event只要分发事件就可以了
- if( e->type() == QEvent::MouseButtonPress)
- {
- //static_cast父与子之间的类型转换
- QMouseEvent * ev = static_cast<QMouseEvent *>(e);
-
- QString str = QString("Event:::鼠标按下了!!! x = %1 y = %2").arg(ev->x()).arg(ev->y());
-
- qDebug() << str;
- //只有鼠标按下 自己处理 返回true
- return true;
- }
-
- //其他事件 让父亲做默认处理
- return QLabel::event(e);
-
- }
samllwidget.cpp
- #include "smallwidget.h"
- #include "ui_smallwidget.h"
-
- smallWidget::smallWidget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::smallWidget)
- {
- ui->setupUi(this);
-
- //QSpinBox 移动, Qslider 跟着移动
- //控件的范围0 -- 99
- void (QSpinBox:: *spSignal) (int )=& QSpinBox::valueChanged;// 函数指针 指向有参函数的函数,参数类型为 int
- connect(ui->spinBox,spSignal,ui->horizontalSlider,&QSlider::setValue);//左边的控件参数 来连接右边的控件
-
- //QSlider 移动 QSpinBox 跟着移动
- connect(ui->horizontalSlider,& QSlider::valueChanged,ui->spinBox,&QSpinBox::setValue);
-
-
-
- }
-
- smallWidget::~smallWidget()
- {
- delete ui;
- }
-
- void smallWidget::setNum(int num)
- {
- ui->spinBox->setValue(num);//设置控件的值 为 num
- }
- int smallWidget::getNum()
- {
- return ui->horizontalSlider->value();//返回当前的值
- }
widget.cpp
- #include "widget.h"
- #include "ui_widget.h"
- #include <QPushButton>
- #include <QDebug>
- #include <QString>
- #include <QTime>
- #include <QTimer>//第二种定时器的头文件
- #include <QMouseEvent>
- #include <QObject>
-
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
-
- //自定义按键封装的信号连接
- connect(ui->btn_get,&QPushButton::clicked,[=](){ //建立按钮的连接
- qDebug()<<ui->widget->getNum();//打印数据
- });
-
- connect(ui->btn_set,&QPushButton::clicked,[=](){
- //数字获取的时候, lineEdit 是空白的时候,我们获取的数字是 0
- unsigned int gg=ui->get_lineEdit->text().toInt();//获取 linEdit 里面的数字
- ui->widget->setNum(gg);
-
- });
-
-
-
-
- //启动定时器 的第一种方法
- id1= startTimer(1000);//参数1 时间间隔 单位 毫秒 一秒加一
- id2=startTimer(2000);//两秒的加1
-
-
-
- //启动定时器的第二种方法
- QTimer * timer1 =new QTimer(this);
- //启动定时器
- timer1->start(500);//参数单位 毫秒
- connect(timer1,&QTimer::timeout,[=](){
- static int num3=1;
- ui->label_time3->setText(QString::number(num3++));
-
- });
-
-
- //给label 安装事件过滤器
- //步骤1 安装事件过滤器
- ui->label->installEventFilter(this);
- ui->label_1->installEventFilter(this);
- //步骤2 重写事件过滤器的事件
-
-
- }
-
- //重写事件过滤器的事件
- bool Widget::eventFilter(QObject *obj,QEvent *e)
- {
-
- if(obj == ui->label|| obj== ui->label_1)//设置规定的标签,设置事件过滤器,,构造函数里面也需要 安装事件过滤器
- {
- if(e->type()==QEvent::MouseButtonPress)
- {
- //static_cast父与子之间的类型转换
- QMouseEvent * ev= static_cast<QMouseEvent *>(e);
- QString str = QString("事件过滤器:::鼠标按下了!!! x = %1 y = %2").arg(ev->x()).arg(ev->y());
- qDebug()<<str;
-
- return true;//true 代表用户自己处理了这个事件,不向下分发
- }
-
- }
- //默认处理
- return QWidget::eventFilter(obj,e);
-
- }
-
-
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- //定时器1 的使用方法
- void Widget::timerEvent(QTimerEvent *ev)
- {
- if(ev->timerId()==id1)
- {
- static int num=1;
- ui->label_time1->setText(QString::number(num++));//把数字转化为字符串
- }
- if(ev->timerId()==id2)
- {
- static int num2=1;
- ui->label_time2->setText(QString::number(num2++));//把数字转化为字符串
- }
-
-
- }
-
-
10.含义:
Qt 创建了 QEvent 事件对象之后,会调用 QObject 的 event()函数做事件的分发。有时候,你可能需要在调用 event()函数之前做一些另外的操作,比如,对话框上某些组件可能并不需要响应回车按下的事件,此时,你就需要重新定义组件的 event()函数。如果组件很多,就需要重写很多次 event()函数,这显然没有效率。为此,你可以使用一个事件过滤器,来判断是否需要调用 event()函数。
QOjbect 有一个 eventFilter()函数,用于建立事件过滤器。这个函数的签名如下:
virtual bool QObject::eventFilter ( QObject * watched, QEvent * event )
事件的流向
QT的各种控件(QObject的子类)都有事件处理成员函数,例如:
- bool QObject::event(QEvent *e);//所有事件
- dragEnterEvent(QDragEnterEvent *);//拖拽进入事件
- focusInEvent(QFocusEvent *);//获得焦点事件
- mousePressEvent(QMouseEvent *);//鼠标压下事件
还有几十个各种类型的事件,不一一列举了,任何一个控件的帮助文件里都可以查到
这些事件需要继承父类重写覆盖之后才能使用,这里主要想说明一点
事件的分发方向,是从子控件一步步向上传递到祖宗控件的,如果子控件拦截了事件,那么父控件就接收不到事件了。子控件怎么拦截事件,怎么不拦截事件。