QWidget 实现 打破布局 或者 当前窗体内的 弹窗 (借助伪造实现)
1 2 3 4 5 6 7 | but = QtWidgets.QToolButton(Dialog2) but.setText( '**' ) but.setAutoRaise( True ) layout.addWidget(but) Dialog2.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.Tool) Dialog2.setWindowOpacity( 1 ) Dialog2.setAttribute(QtCore.Qt.WA_TranslucentBackground) |
Qt.Popup Window Indicates that the widget is a pop-up top-level window, i.e. that it is modal, but has a window system frame appropriate for pop-up menus.
Qt.Tool Indicates that the widget is a tool window. A tool window is often a small window with a smaller than usual title bar and decoration, typically used for collections of tool buttons. If there is a parent, the tool window will always be kept on top of it. If there isn't a parent, you may consider using Qt::WindowStaysOnTopHint as well. If the window system supports it, a tool window can be decorated with a somewhat lighter frame. It can also be combined with Qt::FramelessWindowHint. On OS X, tool windows correspond to the Floating class of windows. This means that the window lives on a level above normal windows; it impossible to put a normal window on top of it. By default, tool windows will disappear when the application is inactive. This can be controlled by the Qt::WA_MacAlwaysShowToolWindow attribute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | from PyQt5 import QtCore, QtGui, QtWidgets class Popup(QtWidgets.QWidget): def __init__( self , parent = None ): super (Popup, self ).__init__(parent ) self .setWindowOpacity( 1 ) # 必须是以顶级弹窗的形式出现,否则最小的长度不好设置 # 最大化 和 最小化的按钮占地方 self .setWindowFlags( QtCore.Qt.Popup | QtCore.Qt.FramelessWindowHint ) self .setAttribute(QtCore.Qt.WA_TranslucentBackground) self .setupUi() # 只能放两个 self ._uis = [] def setupUi( self ): layout_main = QtWidgets.QVBoxLayout( self ) self .setLayout(layout_main) layout_main.setContentsMargins( 0 , 0 , 0 , 0 ) layout_main.setSpacing( 0 ) layout_top = QtWidgets.QHBoxLayout() layout_top_1 = QtWidgets.QHBoxLayout() spacerItem_1 = QtWidgets.QSpacerItem( 2 , 20 , QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self ._layout_top = layout_top_1 layout_top.addLayout(layout_top_1) layout_top.addItem(spacerItem_1) layout_top.setStretch( 1 , 1 ) layout_bottom = QtWidgets.QHBoxLayout() layout_bottom_1 = QtWidgets.QHBoxLayout() self ._layout_bottom = layout_bottom_1 spacerItem_2 = QtWidgets.QSpacerItem( 2 , 20 , QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) layout_bottom.addLayout(layout_bottom_1) layout_bottom.addItem(spacerItem_2) layout_bottom.setStretch( 1 , 1 ) layout_main.addLayout(layout_top) layout_main.addLayout(layout_bottom) # 隐藏事件 的 重新 def hideEvent( self , event): print ( 'hideEvent' ) def addShow( self , ui_1, ui_2): self ._layout_top.addWidget(ui_1) self ._layout_bottom.addWidget(ui_2) self ._uis = [ui_1, ui_1] def showPop(newText): sender = widget.sender() print ( 'newText' , newText, sender.text(), pop.width()) # 需计算长度才行 pop.resize( 80 , 80 ) pop.show() but_1.setFocus() point = input .pos() point_global = input .mapToGlobal(point) pop.move(point_global.x() - point.x(), point_global.y() - point.y()) print (point_global.x(), point_global.y(), point.x(), point.y()) if __name__ = = "__main__" : import sys app = QtWidgets.QApplication(sys.argv) pop = Popup() but_1 = QtWidgets.QLineEdit() but_1.setFixedSize( 50 , 20 ) but_1.setText( 'aa' ) but_2 = QtWidgets.QLineEdit() but_2.setFixedSize( 80 , 80 ) but_2.setText( 'bb' ) pop.addShow(but_1, but_2) print ( 'aa' ) widget = QtWidgets.QWidget() layout = QtWidgets.QVBoxLayout(widget) input = QtWidgets.QLineEdit() layout.addWidget( input ) input2 = QtWidgets.QLineEdit() layout.addWidget(input2) input .textChanged.connect(showPop) widget.show() sys.exit(app.exec_()) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架