简体   繁体   English

Pyqt 小部件 FigureCanvas 边框和内容共存

[英]Pyqt widget FigureCanvas border and content coexist

i want a widget,it can use matplotlib drawing an image and it can setting it's border.我想要一个小部件,它可以使用 matplotlib 绘制图像并且可以设置它的边框。

so i'm create a class,it's parent is FigureCanvas,that i can drawing image.所以我创建了一个类,它的父类是 FigureCanvas,我可以绘制图像。 It seems that setting a border must be rewrite paintEvent method,so i do.好像设置边框必须重写paintEvent方法,所以我这样做了。 But i find when i rewrite paintEvent method,drawing failed.但是我发现当我重写paintEvent方法时,绘制失败了。

When i note the paintEvent method method,drawing success.当我注意到paintEvent方法方法时,绘制成功。

Who can help me improve the code or give me some tips.谁能帮我改进代码或给我一些提示。

import pydicom
from PyQt4 import QtGui
from matplotlib.backends.backend_template import FigureCanvas
from matplotlib.pylab import *
from PyQt4.QtGui import *
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas


class InstancesFC(FigureCanvas):
    def __init__(self):
        self.figure = plt.figure(figsize=(2, 2))
        super(InstancesFC, self).__init__(self.figure)
        self.update_area()
        self.setObjectName('InstancesFC')
        self.ax = self.figure.add_subplot(111)
        plt.axis('off')
        x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        y = [23, 21, 32, 13, 3, 132, 13, 3, 1]
        self.ax.plot(x, y)

    def update_area(self):
        self.setStyleSheet("""
            #InstancesFC{
                          border-width: 1px;
             border-style: solid;
             border-color: red;
             min-width:%s;
             max-width:%s;
             min-height:%s;
             max-height:%s;
             padding:0px;
             margin:1px;
            }
        """ % ('300px', '300px', '300px', '300px'))

    def paintEvent(self, event):
        opt = QStyleOption()
        opt.initFrom(self)
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)


class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):
        h = QtGui.QHBoxLayout()
        h.addWidget(InstancesFC())
        self.setLayout(h)
        self.setGeometry(100, 100, 500, 500)
        self.show()


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

When you override the paintEvent() method and if you don't call the parent method then you will eliminate the default behavior causing the problem you are observing.当您覆盖paintEvent() 方法并且如果您不调用父方法时,那么您将消除导致您所观察到的问题的默认行为。 The solution is to call the paintEvent() method of the parent.解决办法是调用父级的paintEvent()方法。

def paintEvent(self, event):
    super(InstancesFC, self).paintEvent(event)
    opt = QStyleOption()
    opt.initFrom(self)
    painter = QPainter(self)
    painter.setRenderHint(QPainter.Antialiasing)
    self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM