EventFilter
:多用于没有点击信号(clicked
)的部件添加点击信号
操作
- 首先给需要添加事件过滤器的部件注册监听对象;
对象名->installEventFilter(this);
- 重写
eventFilter(QObject *obj, QEvent *event)
函数进行处理。
例:给没有clicked
信号的QComboBox部件增加点击事件,点击QComboBox刷新下拉选项
1 在头文件中添加:
复制代码
1
2
3public slots: bool eventFilter(QObject *watched, QEvent *event);
2 在构造函数中安装QComboBox的事件过滤器:
复制代码
1
2ui->serialPortCbx->installEventFilter(this);
3 实现函数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15bool Serial_port_test::eventFilter(QObject *watched, QEvent *event) { if(event->type() == QEvent::MouseButtonPress){ if(watched == ui->serialPortCbx){ QComboBox * combobox = qobject_cast<QComboBox *>(watched); combobox->clear(); combobox->addItems("test 1"); combobox->addItems("test 2"); } return true; } return false; }
最后
以上就是野性洋葱最近收集整理的关于【Qt】EventFilter(事件过滤器)的全部内容,更多相关【Qt】EventFilter(事件过滤器)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复