【问题标题】:Painting QGraphicsPixmapItem border on hover在悬停时绘制 QGraphicsPixmapItem 边框
【发布时间】:2019-05-22 23:21:03
【问题描述】:

我正在尝试创建自己的 QGraphicsPixmapItem,我可以在悬停模式下激活,并且我想在将鼠标悬停在项目的矩形上时绘制黑色边框,并在离开矩形空间时恢复正常。

我启动了这段代码,但不知道下一步该做什么。还想做一个paintEvent,但QGraphicsPixmapItems 没有。所以我更加困惑,因为不认为绘画方法会相同。

class PixmapItem(QGraphicsPixmapItem):
    def __init__(self, pixmap, rect, parent=None):
        super().__init__(parent)
        self.pixmap = pixmap
        self.setPixmap(self.pixmap)
        self.rect = rect
        self.setAcceptHoverEvents(True)

    def hoverEnterEvent(self, *args, **kwargs):
        pass

我可以让悬停打印“你好”,但不能做任何其他事情,即使有一些例子,因为这些是与 paintEvent 和其他类型的项目。

如果可能的话,我想保持项目的类型并按照我所说的绘制边框。但也不知道这是否会是一个更好的方法,也很简单。

【问题讨论】:

    标签: python pyqt pyqt5 qgraphicspixmapitem


    【解决方案1】:

    QGraphicsItem 没有paintEvent 方法,而是paint() 方法:

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class PixmapItem(QtWidgets.QGraphicsPixmapItem):
        def __init__(self, pixmap, parent=None):
            super().__init__(pixmap, parent)
            self.setAcceptHoverEvents(True)
            self._is_hovered = False
    
        def hoverEnterEvent(self, event):
            self._is_hovered = True
            self.update()
            super().hoverEnterEvent(event)
    
        def hoverLeaveEvent(self, event):
            self._is_hovered = False
            self.update()
            super().hoverLeaveEvent(event)
    
        def paint(self, painter, option, widget=None):
            super().paint(painter, option, widget)
            if self._is_hovered:
                painter.save()
                pen = QtGui.QPen(QtGui.QColor("black"))
                pen.setWidth(4)
                painter.setPen(pen)
                painter.drawRect(self.boundingRect())
                painter.restore()
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
    
        scene = QtWidgets.QGraphicsScene()
        view = QtWidgets.QGraphicsView(scene)
        item = PixmapItem(QtGui.QPixmap("image.png"))
        scene.addItem(item)
        view.resize(640, 480)
        view.show()
        sys.exit(app.exec_())
    

    更新:

    def paint(self, painter, option, widget=None):
        super().paint(painter, option, widget)
        if self._is_hovered:
            painter.save()
            pen = QtGui.QPen(QtGui.QColor("black"))
            pen.setWidth(4)
            painter.setPen(pen)
            r = self.boundingRect()
            r.adjust(0, 0, -pen.width()/2, -pen.width()/2)
            painter.drawRect(r)
            painter.restore()
    

    【讨论】:

    • 如果没有定义 QRect,paint 方法如何知道边界矩形在哪里?对我来说没有意义。就在这周内,我一直在使用 PyQt 进行编程,所以这一切对我来说都是全新的。
    • @BlackMage 所有 QGraphicsItem 都定义了 boundingRect,在 QGraphicsPixmapItem 的情况下是占据像素图的矩形。我建议你阅读doc.qt.io/qt-5/graphicsview.html
    • 好的,谢谢!但还有一件事是我的图像中的宽度在右边界和下边界是不同的,它们更薄。那是因为我的基本代码/像素图?
    • @BlackMage 你的图片有多大?
    • 这是一个 16x16 png 图像,在我的程序中缩放为 32x32。
    猜你喜欢
    • 2019-01-15
    • 1970-01-01
    • 2021-08-29
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    相关资源
    最近更新 更多