【问题标题】:Create pyside application in different shape than rectangle创建与矩形不同形状的 pyside 应用程序
【发布时间】:2018-09-21 10:01:41
【问题描述】:

我正在 Pyside2 中构建一个应用程序。

我们知道,当我们将主窗口用于我们的应用程序时,它带有矩形形状。

但我希望用户定义应用程序的形状。

例如查看下面的 Zoiper 应用程序图片。

背景是我的编辑器加上一些文字,你可以很容易地感觉到应用程序的外边框。

我们可以使用 pyside2 实现同样的目标吗?

提前致谢。

【问题讨论】:

    标签: python pyside2


    【解决方案1】:

    创建一个透明的小部件。有两行很重要:

    self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool)
    self.setAttribute(Qt.WA_TranslucentBackground)
    

    工作示例:

    from PySide2.QtCore import Qt
    from PySide2.QtGui import QColor, QPainterPath, QPainter
    from PySide2.QtWidgets import QApplication, QWidget
    
    
    class WCustomShape(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool)
            self.setFixedSize(400, 300)
            self.setAttribute(Qt.WA_TranslucentBackground)
    
        def paintEvent(self, e):
            painter = QPainter(self)
            path = QPainterPath()
            path.addEllipse(200, 150, 100, 50)
            painter.fillPath(path, QColor(Qt.blue))
    
    
    if __name__ == '__main__':
        import sys
    
        app = QApplication(sys.argv)
    
        main_window = WCustomShape()
        main_window.show()
        main_window.raise_()
    
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 2022-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 2016-04-14
      • 1970-01-01
      相关资源
      最近更新 更多