简体   繁体   English

问:如何在QFormLayout中悬停行时收到通知?

[英]Qt: How to be notified when a row is hovered in a QFormLayout?

I have a QFormLayout where the left widgets are QLabels and the right widgets are of various types. 我有一个QFormLayout,左边的小部件是QLabel,右边的小部件是各种类型的。 I want to get notified when the mouse enters any part of a form-row, so I can display an explanation of that row in my statusbar. 我想在鼠标进入表格行的任何部分时收到通知,因此我可以在状态栏中显示该行的说明。

Currently I have a QLabel subclass called HoverableLabel which exposes "mouseEntered" and "mouseLeft" signals (emitted in my reimplementations of enterEvent and leaveEvent). 目前我有一个名为HoverableLabel的QLabel子类,它暴露了“mouseEntered”和“mouseLeft”信号(在我重新实现的enterEvent和leaveEvent中发出)。 This works, but: 这有效,但是:

  • The margins between the rows don't trigger the signals 行之间的边距不会触发信号
  • The space on the left of the (right-aligned) labels doesn't trigger the signal (右对齐)标签左侧的空间不会触发信号
  • The widgets on the right don't trigger the signal because I haven't bothered to subclass all of them 右边的小部件不会触发信号,因为我没有费心去分类所有这些信号

What's the Qt-blessed approach to this kind of problem? 什么是Qt祝福这种问题的方法?

Some things I can think of: 我能想到的一些事情:

  1. Make the formlayout's parent a widget that filters all mousemove events (mouse tracking?) and checks if the mouse has entered some row. 使formlayout的父窗口小部件过滤所有mousemove事件(鼠标跟踪?)并检查鼠标是否已进入某行。
  2. Change the formlayout into a QVBoxLayout, and make the rows into some custom widget like FormRowWidget which handles both hover events and the form-alignment stuff. 将formlayout更改为QVBoxLayout,并将行设置为一些自定义窗口小部件,如FormRowWidget,它处理悬停事件和表单对齐的内容。

Neither is very nice. 两者都不是很好。

I ended up using the first of the two solutions. 我最终使用了两个解决方案中的第一个。 Good enough, and with some work it can be isolated into a reusable subclass of QFormLayout. 足够好,通过一些工作,它可以被隔离成QFormLayout的可重用子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM