自己实现的qgraphicsitem

import sys
from PyQt5 import QtWidgets, QtGui, QtCore


class PingLeiItem(QtWidgets.QGraphicsItem):
    def __init__(self):
        QtWidgets.QGraphicsItem.__init__(self)

        self.setFlags(QtWidgets.QGraphicsItem.ItemIsSelectable |
                      QtWidgets.QGraphicsItem.ItemIsMovable)
        self.prepareGeometryChange()

        self.width = 100
        self.height = 25
        self.pingleiColor = QtGui.QColor(100, 100, 100)
        self.pingleiText = "abc"

    def boundingRect(self):
        return QtCore.QRectF(0, 0, self.width, self.height)

    def paint(self, painter, option, widget):
        pen = QtGui.QPen()
        pen.setWidthF(1)
        pen.setColor(self.pingleiColor)
        brushColor = self.pingleiColor
        if option.state & QtWidgets.QStyle.State_Selected:
            brushColor = self.pingleiColor.darker(150)
        painter.setPen(pen)
        painter.setBrush(QtGui.QBrush(brushColor))
        painter.drawRect(0, 0, 100, 25)
        pen.setColor(QtGui.QColor(255, 255, 255))
        painter.setPen(pen)
        painter.drawText(10, 10, self.pingleiText)


class DanPingItem(QtWidgets.QGraphicsItem):
    def __init__(self):
        QtWidgets.QGraphicsItem.__init__(self)

        self.setFlags(QtWidgets.QGraphicsItem.ItemIsSelectable)
        self.setAcceptHoverEvents(True)

        self.danPingColor = QtGui.QColor(100, 100, 100)
        self.danPingText = "abc"

    def boundingRect(self):
        return QtCore.QRectF(0, 0, 100, 40)

    def paint(self, painter, option, widget):
        pen = QtGui.QPen()
        pen.setWidthF(1)
        pen.setColor(self.danPingColor)
        brushColor = self.danPingColor
        if option.state & QtWidgets.QStyle.State_Selected:
            brushColor = self.danPingColor.darker(150)
        if option.state & QtWidgets.QStyle.State_MouseOver:
            brushColor = self.danPingColor.darker(110)
        painter.setPen(pen)
        painter.setBrush(QtGui.QBrush(brushColor))
        painter.drawRect(0, 0, 100, 40)
        pen.setColor(QtGui.QColor(255, 255, 255))
        painter.setPen(pen)
        painter.drawText(35, 10, self.danPingText)
        painter.drawPixmap()


class Window(QtWidgets.QGraphicsView):
    def __init__(self):
        QtWidgets.QGraphicsView.__init__(self)

        self.resize(1000, 800)
        myScene = QtWidgets.QGraphicsScene()
        self.setScene(myScene)

        item1 = PingLeiItem()
        item2 = DanPingItem()
        # myScene.addItem(item1)
        myScene.addItem(item2)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值