qt如何向子窗口发送send/post自定义事件

barryzhanghu 2020-02-28 05:29:54
最近需要更改一个GUI,原eventfilter安装在mainwindow中ui->boardView 下监听左右键,但是我需要将监听的事件替换为自定义事件 (当某个变量超过阈值)。但是在main.cpp中我需要手动send事件,但是只能send给mainwindow w, 而不能到ui->boardView,事件监测器也无法监听事件。相关代码可见于https://stackoverflow.com/questions/60424216/how-to-use-eventfilter-under-child-widget-to-catch-self-define-event 或知乎https://www.zhihu.com/question/375221137
...全文
610 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
barryzhanghu 2020-03-05
  • 打赏
  • 举报
回复
怎么做呢?return吗?
非弋 2020-03-05
  • 打赏
  • 举报
回复
引用 5 楼 barryzhanghu 的回复:
怎么做呢?return吗?
就是return啊,你试一下就知道了
非弋 2020-02-29
  • 打赏
  • 举报
回复
引用 2 楼 barryzhanghu 的回复:
问题是ui 定义在mainwindow内,一旦我在main.cpp中修改为QCoreApplication::postEvent(ui->boardView, .....), 编译的时候就报错 ui:used of undeclared identifier, 有什么办法可以解决呢?
如果只是简单的解决这个编译报错,你可以在MainWindow中写一个方法将ui->boardView对象抛出来
donwmufromdying 2020-02-29
  • 打赏
  • 举报
回复
你估计没有真正了解event机制。一个对象要能接收并处理event首先要继承自QObject,其次要自己实现event(),第三:该对象要启动eventloop.一般有eventloop的类基本上都有这么一个函数启动eventloop:exec()
非弋 2020-02-28
  • 打赏
  • 举报
回复
5:29发的帖子 void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority) 第一个参数是接收者的对象 修改为QCoreApplication::postEvent(ui->boardView, .....),ui->boardView窗口才可以接收到事件
barryzhanghu 2020-02-28
  • 打赏
  • 举报
回复
问题是ui 定义在mainwindow内,一旦我在main.cpp中修改为QCoreApplication::postEvent(ui->boardView, .....), 编译的时候就报错 ui:used of undeclared identifier, 有什么办法可以解决呢?

16,239

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧