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

 

相关文章
|
1月前
|
存储 开发框架 开发者
QT C++焦点事件:多角度解析实用技巧与方法
QT C++焦点事件:多角度解析实用技巧与方法
146 0
|
5月前
|
JSON 小程序 Android开发
小程序列表左滑效果在IOS呈上下滑动影响样式
小程序列表左滑效果在IOS呈上下滑动影响样式
97 0
Qml实用技巧:在可视元素之前半透明覆盖一个可视元素,阻止鼠标透(界面)传(防止点击到被遮挡的按钮)
Qml实用技巧:在可视元素之前半透明覆盖一个可视元素,阻止鼠标透(界面)传(防止点击到被遮挡的按钮)
Qml实用技巧:在可视元素之前半透明覆盖一个可视元素,阻止鼠标透(界面)传(防止点击到被遮挡的按钮)
|
4月前
|
开发工具 索引
点击一个消除游戏图标时,背后都发生了什么
点击一个消除游戏图标时,背后都发生了什么
32 1
|
4月前
|
索引
消除游戏中图标下落的原理和实现
消除游戏中图标下落的原理和实现
17 1
|
11月前
摇杆控制移动自动瞄准射击如何实现?
前言 这篇教程实现了射击游戏中的摇杆控制移动以及自动瞄准射击功能,并提供一个示例项目的模版。
94 0
|
11月前
|
Java
手把手一步一步教你使用Java开发一个大型街机动作闯关类游戏20之enemy被攻击显示后退动画(block效果)
手把手一步一步教你使用Java开发一个大型街机动作闯关类游戏20之enemy被攻击显示后退动画(block效果)
134 0
C++ 使得窗口置顶(终极方案)
C++ 使得窗口置顶(终极方案)
567 0
|
Web App开发 安全 JavaScript
Chrome 92 破坏性功能,我这弹窗有何用?
近期,Chrome 92 进行了发布,我们来看看 Chrome 92 中提及的一个影响比较大的破坏性改动。
Chrome 92 破坏性功能,我这弹窗有何用?
|
前端开发
小视频源码,可控的跑马灯,无需焦点
小视频源码,可控的跑马灯,无需焦点
259 0

热门文章

最新文章