PyQt - 我可以在paintEvent中添加一个QWidget吗?

时间:2015-02-12 13:27:06

标签: pyqt pyqt4

我正在使用PyQt创建一个应用程序,除其他外,它以简单的图形图形式显示信息。

我已经将其作为QWidget完成并重新实现了paintEvent方法来进行绘制。下面将展示一个小而非常简洁的例子。

from PyQt4.QtCore import * 
from PyQt4.QtGui import *    # sloppy, I know, but done for speed! :-)

class Example(QMainWindow):
    def __init__(self,parent=None):
        super(Example,self).__init__(parent)
        self.init_ui()

    def init_ui(self):
        base=QWidget()
        layout=QVBoxLayout(base)
        self.diagram=Diagram()
        layout.addWidget(self.diagram)
        self.setCentralWidget(base)

class Diagram(QWidget):
    def __init__(self,parent=None):
        super(Diagram,self).__init__(parent)
        self.width=360
        self.height=120
        self.leftMargin=10
        self.topMargin=10

    def paintEvent(self,event=None):
        painter=QPainter(self)
        painter.setWindow(self.leftMargin,self.topMargin,self.width,self.height)
        self.drawConnection(painter, 40, 25, 40, 90)
        self.drawConnection(painter, 40, 90, 40, 110)
        self.drawConnection(painter, 40, 110, 200, 100)
        self.drawItem(painter, 40, 40)
        self.drawItem(painter, 40, 90)
        self.drawState(painter,200,100)

    def drawConnection(self,painter,x0,y0,x1,y1):
        pen=QPen()
        pen.setWidth(4)
        pen.setColor(QColor(50,50,200))
        painter.setPen(pen)
        painter.drawLine(x0,y0,x1,y1)

    def drawItem(self,painter,x,y):
        w=40
        h=30
        r=QRectF(x-(w/2),y-(h/2),w,h)
        painter.drawRoundedRect(r,5.0,5.0)
        grad=QLinearGradient(QPointF(15.0,20.0),QPointF(30.0,30.0))
        pen=QPen()
        pen.setWidth(2)
        pen.setColor(QColor(10,10,10))
        painter.setPen(pen)
        brush=QBrush(QColor(0,200,10))
        painter.setBrush(brush)
        painter.drawRoundedRect(r,5.0,5.0)

    def drawState(self,painter,x,y):
        w=80
        h=60
        r=QRectF(x-(w/2),y-(h/2),w,h)
        pen=QPen()
        pen.setWidth(2)
        pen.setColor(QColor(255,10,10))
        painter.setPen(pen)
        brush=QBrush(QColor(200,200,10))
        painter.setBrush(brush)
        painter.drawPie(r,5040,1440)

if __name__=='__main__':
    app=QApplication(sys.argv)
    example=Example()
    example.show()
    app.exec_()

是否可以添加QWidget而不是调用drawItem?

例如,适当样式的QPushButton可以代替圆角矩形。

或者,创建包含小部件和绘制项目的显示的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

我使用QSS编写了一个圆形子窗口小部件示例。

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys


ROUNDED_STYLE_SHEET = """QPushButton {
     background-color: red;
     border-style: outset;
     border-width: 2px;
     border-radius: 10px;
     border-color: beige;
     font: bold 14px;
     min-width: 10em;
     padding: 6px;
 }
"""


class MyWidget(QWidget):
    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)
        self.mChildWidget = QPushButton(self)
        self.resize(600, 400)
        self.mChildWidget.resize(120, 80)
        self.mChildWidget.move(300, 200)
        self.mChildWidget.setStyleSheet(ROUNDED_STYLE_SHEET)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    app.exec_()