PySidePyQt覆盖小部件

PySidePyQt覆盖小部件,第1张

PySide / PyQt覆盖部件

多亏了armatita,我才能得到想要的东西。目前,存在一些问题,但是它可以工作,并且我得到了想要的结果。

我将代码提供给下一个将要寻找相同内容的人。

from PySide import QtCore, QtGuiimport sysclass CtmWidget(QtGui.QWidget):    def __init__(self, parent = None):        QtGui.QWidget.__init__(self, parent)        self.button = QtGui.QPushButton("Close Overlay")        self.setLayout(QtGui.QHBoxLayout())        self.layout().addWidget(self.button)        self.button.clicked.connect(self.hideOverlay)    def paintEvent(self, event):        painter = QtGui.QPainter()        painter.begin(self)        painter.setRenderHint(QtGui.QPainter.Antialiasing)        path = QtGui.QPainterPath()        path.addRoundedRect(QtCore.QRectF(self.rect()), 10, 10)        mask = QtGui.QRegion(path.toFillPolygon().toPolygon())        pen = QtGui.QPen(QtCore.Qt.white, 1)        painter.setPen(pen)        painter.fillPath(path, QtCore.Qt.white)        painter.drawPath(path)        painter.end()    def hideOverlay(self):        self.parent().hide()class Overlay(QtGui.QWidget):    def __init__(self, parent, widget):        QtGui.QWidget.__init__(self, parent)        palette = QtGui.QPalette(self.palette())        palette.setColor(palette.Background, QtCore.Qt.transparent)        self.setPalette(palette)        self.widget = widget        self.widget.setParent(self)    def paintEvent(self, event):        painter = QtGui.QPainter()        painter.begin(self)        painter.setRenderHint(QtGui.QPainter.Antialiasing)        painter.fillRect(event.rect(), QtGui.QBrush(QtGui.QColor(0, 0, 0, 127)))        painter.end()    def resizeEvent(self, event):        position_x = (self.frameGeometry().width()-self.widget.frameGeometry().width())/2        position_y = (self.frameGeometry().height()-self.widget.frameGeometry().height())/2        self.widget.move(position_x, position_y)        event.accept()class MainWindow(QtGui.QWidget):    def __init__(self):        QtGui.QWidget.__init__(self)        self.resize(800, 500)        self.button = QtGui.QPushButton("Click Me")        self.setLayout(QtGui.QVBoxLayout())        self.layout().addWidget(self.button)        self.popup = Overlay(self, CtmWidget())        self.popup.hide()        # Connections        self.button.clicked.connect(self.displayOverlay)    def displayOverlay(self):        self.popup.show()        print "clicked"    def resizeEvent(self, event):        self.popup.resize(event.size())        event.accept()if __name__ == "__main__":    app = QtGui.QApplication(sys.argv)    window = MainWindow()    window.show()    sys.exit(app.exec_())

再次感谢你们(ymmx和armatita),花时间在我的问题上。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5631897.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存