我是靠谱客的博主 野性洋葱,这篇文章主要介绍【Qt】EventFilter(事件过滤器),现在分享给大家,希望可以做个参考。


EventFilter:多用于没有点击信号(clicked)的部件添加点击信号


操作

  1. 首先给需要添加事件过滤器的部件注册监听对象;
    对象名->installEventFilter(this);
  2. 重写eventFilter(QObject *obj, QEvent *event)函数进行处理。


例:给没有clicked信号的QComboBox部件增加点击事件,点击QComboBox刷新下拉选项


1 在头文件中添加:

public slots:
	bool eventFilter(QObject *watched, QEvent *event);

2 在构造函数中安装QComboBox的事件过滤器:

ui->serialPortCbx->installEventFilter(this);

3 实现函数

bool 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(事件过滤器)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(114)

评论列表共有 0 条评论

立即
投稿
返回
顶部