QWidget 实现 打破布局 或者 当前窗体内的 弹窗 (借助伪造实现)

简介:   but = QtWidgets.QToolButton(Dialog2) but.setText('**') but.setAutoRaise(True) layout.

 

 

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.

 

 

 

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_())

 

相关文章
|
数据采集 SQL 数据可视化
大数据可视化技巧:借助PowerBI提升数据故事讲述力
【4月更文挑战第8天】Power BI助力大数据可视化,支持多种数据源连接,如SQL Server、Excel,提供数据清洗与转换功能。通过选择合适图表类型、运用颜色和大小强化表达,创建交互式仪表板。讲述数据故事时,注重故事主线设计,利用叙事技巧引导观众,并添加文本说明。分享已完成报告,提升数据驱动决策能力。动手实践,体验Power BI的强大与易用。
497 0
|
监控 前端开发 JavaScript
Qt Quick调试之道:跟踪、输出与打印信息的全面攻略
Qt Quick调试之道:跟踪、输出与打印信息的全面攻略
954 0
|
测试技术 网络架构
SFP与GBIC详解:探索两者的细微差别
【4月更文挑战第21天】
478 0
|
9月前
|
机器人 API 定位技术
具身智能干货|ROS2理论与实践系列(二):ROS2通信机制核心
机器人是一种高度复杂的系统性实现,一个完整的机器人应用程序可能由若干功能模块组成,每个功能模块可能又包含若干功能点,在不同功能模块、不同功能点之间需要频繁的进行数据交互。比如以导航中的路径规划模块为例: 路径规划时就需要其他功能模块输入数据,并输出数据以被其他模块调用。 输入的数据有地图服务提供的地图数据、定位模块提供的机器人位姿数据、人机交互模块提供的目标点数据......。 输出的路径信息则被运动控制订阅或是回显在人机交互界面上。 那么这些相对独立的功能模块或功能点之间是如何实现数据交互的呢?在此,我们就需要介绍一下ROS2中的通信机制了。
1001 62
|
域名解析 网络协议 安全
DNS隧道
【8月更文挑战第20天】
574 1
|
Web App开发 JavaScript 前端开发
Qt(QtWebEngine)加载本地网页跨域问题的总结
Qt(QtWebEngine)加载本地网页跨域问题的总结
422 0
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 表单布局Form Layout
【Qt 学习笔记】Qt常用控件 | 布局管理器 | 表单布局Form Layout
571 2
Pyside6-第二篇-QPushButton一个普通按钮
Pyside6-第二篇-QPushButton一个普通按钮
558 0
|
Linux Shell 数据处理
深入了解Linux的printenv命令:环境变量的查看与解析
`printenv`命令在Linux中用于查看环境变量,它显示所有或特定变量及其值。该命令简单易用,可结合参数实现过滤和筛选,如 `-0` 用于null分隔,`-n` 只列名称,`-u` 查找未设置的变量。通过与`grep`等结合,能进行复杂查询。在使用时要注意保护敏感信息,并理解变量对系统和程序的影响。定期检查和管理环境变量能提升系统效能和安全性。
|
算法 Linux 编译器
【C++ 泛型编程 进阶篇】 C++ 模版元编程 类型转换 std::decay 全面教程
【C++ 泛型编程 进阶篇】 C++ 模版元编程 类型转换 std::decay 全面教程
580 0