Popup的一个应用.

本文介绍了用Popup Window做web菜单的好处,如带继承性、隐藏特性好、不被覆盖等。同时指出应用中存在的问题,包括内存问题、显示闪烁、样式附加、内容引用、事件处理、逻辑控制等,并给出相应解决建议,最后欢迎交流。

    好多地方已经提到了可以用Popup Window来做web上的菜单,好处有很多:
    1.Popup带继承性,就是说同级Popup只能存在一个,但可以有子Popup,孙Popup,重孙Popup....,这个跟菜单-子菜单-孙菜单,重孙菜单......很相似,哈哈,这一家子还要注意是一脉单传的,不可能有兄弟同堂的情况.
     2.Popup链中一个Popup隐藏,所有该Popup下面的窗口全部隐藏.这个特性真的让我们方便不少.
    3.它不会被别的窗体或者控件覆盖,对于以往的DIV,这个问题还真有点麻烦.
    4.点击菜单Popup之外的任意地方,Popup自动隐藏.
    5.其他暂时还没有想到,倒是在应用当中碰到了不少问题,下面列出来大家可以参考一下.
  
    问题:
   1.内存问题.这个是很重要的,基本上对一个Popup设为Null以后,在内存里是不释放的,所以如果不注意,它的内存会越用越多.最后导致浏览器无法工作.发现这个问题也是由于我比较偷懒,对于菜单的隐藏只是简单的设置为null了之.实际上,比较好的方法是这样:同级菜单只使用一个Popup,且一直保留在内存里,下级子菜单是为这个Popup创建子Popup.同级菜单要切换,只是简单的更换窗口的内容(HTML)而已.比如,一个菜单总共4层,那就创建4逐步继承的窗口,而不是在于它里面总共有多少菜单,为每个菜单创建一个窗口.
    2.对于Popup显示的问题,尽量不要用先show(0,0,1,1,obj),然后hide(),最后在show(实际尺寸),因为这样会导致菜单会闪.看着晃眼.最好事先把定位和尺寸算准了再show.
    3.如果你打算用外联CSS样式表的话,Popup需要用oNewChildWin.document.createStyleSheet('Menu.css')来动态的附加CSS样式表.当然,如果在代码里面加也一样.
    4.对于任何一个Popup里面内容的引用,需要加入Popup的名字作为引用对象.浏览器的原有窗口需要加“window“前缀.  
    5.对于离开有下级菜单的菜单项的时候,尽量不要用onmouseout来隐藏该菜单项的子菜单,而是用进入到别的菜单项的onmouseover的事件来隐藏子菜单,如果用onmouseout来隐藏该菜单项的子菜单,逻辑控制会变复杂.
    6.我在使用Popup对象的时候没有取到event对象,可能它本身不提供.所以我们没法用event.srcElement来取得事件的触发对象.所以尽量用参数来传递对象.如果你要attchEvent的时候在函数里面传递参数,可以这么写:
       obj.attachEvent(“onmouseover“,function(){
             fnTrueFunction(obj); 
       });
    7.菜单隐藏显示的逻辑是比较麻烦的事情,做之前还是要写一个流程.

    Popup做菜单我想到的是这些,欢迎大家跟我交流. 
   祝大家使用Popup愉快.
     

这是一个非常常见的问题:**在 PyQt 中,当弹窗 (`QTextEdit`) 显示为 `Qt.Popup` 类型时,点击外部区域无法正常触发关闭逻辑**。这是因为 `mousePressEvent` 是在弹窗内部处理的,而外部区域的点击事件会被弹窗捕获。 --- ## ✅ 问题原因 在你当前的代码中: ```python self.popup.mousePressEvent = close_popup ``` 这行代码只是重写了 `QTextEdit` 的 `mousePressEvent`,**只有在弹窗内部点击时才会触发**。当你点击弹窗外部区域时,事件由其他控件处理,而弹窗会收到这个事件,所以触发 `close_popup()`。 --- ## ✅ 解决方案:使用 `QApplication.focusChanged` 或 `QEvent.ApplicationDeactivate` 要解决这个问题,你可以使用以下两种方式之一来监听外部点击事件: --- ### ✅ 方法一:使用 `QApplication.focusChanged` 信号(推荐) 当焦点离开弹窗时,说明用户点击了别的地方。 ```python from PyQt5.QtWidgets import QTextEdit, QItemDelegate, QApplication from PyQt5.QtCore import Qt, QMouseEvent class PopupTextDelegate(QItemDelegate): def __init__(self, parent=None): super().__init__(parent) self.popup = None def editorEvent(self, event, model, option, index): if event.type() == QMouseEvent.MouseButtonPress: text = index.data(Qt.DisplayRole) rect = option.rect self.popup = QTextEdit() self.popup.setWindowFlags(Qt.Popup) self.popup.setReadOnly(True) self.popup.setPlainText(text) self.popup.setGeometry(rect.x(), rect.bottom(), rect.width(), 100) self.popup.show() # 使用 focusChanged 信号检测外部点击 def on_focus_changed(old, new): if self.popup and (new is None or not self.popup.geometry().contains(QApplication.desktop().cursor().pos())): self.popup.close() self.popup = None QApplication.instance().focusChanged.connect(on_focus_changed) # 保存连接以便后续清理(防止多次连接) self.popup.focusChanged = lambda old, new: on_focus_changed(old, new) return True return super().editorEvent(event, model, option, index) ``` --- ### ✅ 方法二:使用 `QEvent.ApplicationDeactivate`(适用于窗口失去焦点) 当整个应用程序失去焦点时关闭弹窗: ```python from PyQt5.QtCore import QEvent class PopupTextDelegate(QItemDelegate): def __init__(self, parent=None): super().__init__(parent) self.popup = None def editorEvent(self, event, model, option, index): if event.type() == QMouseEvent.MouseButtonPress: text = index.data(Qt.DisplayRole) rect = option.rect self.popup = QTextEdit() self.popup.setWindowFlags(Qt.Popup) self.popup.setReadOnly(True) self.popup.setPlainText(text) self.popup.setGeometry(rect.x(), rect.bottom(), rect.width(), 100) self.popup.show() def close_popup(): if self.popup: self.popup.close() self.popup = None # 当应用失去焦点时关闭弹窗 QApplication.instance().installEventFilter(self) self.popup.close_popup = close_popup # 保存引用 return True return super().editorEvent(event, model, option, index) def eventFilter(self, obj, event): if event.type() == QEvent.ApplicationDeactivate and self.popup: self.popup.close() self.popup = None return True return super().eventFilter(obj, event) ``` --- ## ✅ 补充建议 - **避免多次绑定信号**:每次点击都绑定 `focusChanged` 会导致重复连接,建议在类中保存引用并只绑定一次。 - **弹窗复用**:频繁创建 `QTextEdit` 会影响性能,可以考虑复用弹窗。 - **使用 QDialog + QLabel**:如果你只是展示文本,使用 `QDialog` + `QLabel` + `QScrollArea` 更轻量。 --- ## ✅ 示例优化:使用 `QLabel` + `QDialog` 实现弹窗 ```python from PyQt5.QtWidgets import QDialog, QLabel, QScrollArea, QVBoxLayout from PyQt5.QtCore import Qt class TextPopup(QDialog): def __init__(self, text, parent=None): super().__init__(parent, Qt.Popup) self.setModal(False) self.resize(300, 150) label = QLabel(text) label.setWordWrap(True) label.setTextInteractionFlags(Qt.TextSelectableByMouse) scroll = QScrollArea() scroll.setWidget(label) scroll.setWidgetResizable(True) layout = QVBoxLayout() layout.addWidget(scroll) self.setLayout(layout) def mousePressEvent(self, event): if not self.rect().contains(event.pos()): self.close() ``` 然后在 `editorEvent` 中使用: ```python self.popup = TextPopup(text) self.popup.show() ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值