Python 正在剪裁QGraphicsSellipseitem绘制事件

Python 正在剪裁QGraphicsSellipseitem绘制事件,python,pyside,Python,Pyside,我这里有两个问题 当我绘制椭圆时,在悬停事件期间,椭圆的边缘似乎没有被绘制成白色。当圆点是它的常规大小时,以及当它悬停在上面时,我如何解决这个问题 当用户将鼠标悬停在圆点上时,我希望圆点的半径增加2,但是当我这样做时,它当前不会从圆点的中心缩放。如何调整,使其基于圆点的中心点增大大小 导入系统 从PySide.QtGui导入* 从PySide.QtCore导入* 随机输入 类MyGraphicsView(QGraphicsView): 定义初始化(自): 超级(MyGraphicsView,s

我这里有两个问题

  • 当我绘制椭圆时,在悬停事件期间,椭圆的边缘似乎没有被绘制成白色。当圆点是它的常规大小时,以及当它悬停在上面时,我如何解决这个问题
  • 当用户将鼠标悬停在圆点上时,我希望圆点的半径增加2,但是当我这样做时,它当前不会从圆点的中心缩放。如何调整,使其基于圆点的中心点增大大小
  • 导入系统 从PySide.QtGui导入* 从PySide.QtCore导入* 随机输入 类MyGraphicsView(QGraphicsView): 定义初始化(自): 超级(MyGraphicsView,self)。\uuuu init\uuuuu() self.setDragMode(QGraphicsView.RubberBandDrag) self.\u isPanning=False self.\u mousePressed=False #self.setBackgroundBrush(QImage(“C:/Users/jmartini/Desktop/Temp/images/flag_0140.jpg”)) self.setCacheMode(QGraphicsView.CacheBackground) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) def鼠标压力事件(自身、事件): 如果event.button()==Qt.LeftButton: self.\u mousePressed=True 如果自行规划: self.setCursor(Qt.ClosedHandCursor) self.\u dragPos=event.pos() event.accept() 其他: 超级(MyGraphicsView,self).mousePressEvent(事件) elif event.button()==Qt.MiddleButton: self.\u mousePressed=True self.\u isPanning=True self.setCursor(Qt.ClosedHandCursor) self.\u dragPos=event.pos() event.accept() def mouseMoveEvent(自身、事件): 如果按下self.\u鼠标并进行self.\u扫描: newPos=event.pos() diff=新位置-自 self.\u dragPos=newPos self.horizontalScrollBar().setValue(self.horizontalScrollBar().value()-diff.x()) self.verticalScrollBar().setValue(self.verticalScrollBar().value()-diff.y()) event.accept() 其他: 超级(MyGraphicsView,self).mouseMoveEvent(事件) def mouseReleaseEvent(自身,事件): 如果event.button()==Qt.LeftButton: 如果自行规划: self.setCursor(Qt.OpenHandCursor) 其他: self.\u isPanning=False self.setCursor(Qt.ArrowCursor) self.\u mousePressed=False elif event.button()==Qt.MiddleButton: self.\u isPanning=False self.setCursor(Qt.ArrowCursor) self.\u mousePressed=False 超级(MyGraphicsView,self).mouseReleaseEvent(事件) def mouseDoubleClickEvent(自身,事件): self.fitInView(self.scen直立(),Qt.KeepAspectRatio) 通过 def按键事件(自身,事件): 如果event.key()==Qt.key\u空格和非self.\u鼠标按下: self.\u isPanning=True self.setCursor(Qt.OpenHandCursor) 其他: 超级(MyGraphicsView,自我)。按键事件(事件) def钥匙释放事件(自身,事件): 如果event.key()==Qt.key\u空间: 如果不是self.\u鼠标按下: self.\u isPanning=False self.setCursor(Qt.ArrowCursor) 其他: 超级(MyGraphicsView,自我)。按键事件(事件) def wheelEvent(自我,事件): #缩放因子 系数=1.25 #锚定 self.setTransformationAnchor(QGraphicsView.NoAnchor) self.setResizeAnchor(QGraphicsView.NoAnchor) #保存场景pos oldPos=self.maptosene(event.pos()) #变焦 如果event.delta()小于0: 系数=1.0/系数 自我尺度(因子,因子) #获得新职位 newPos=self.maptosene(event.pos()) #将场景移动到旧位置 增量=新位置-旧位置 self.translate(delta.x(),delta.y()) 类别MyGraphicscene(QGraphicscene): 定义初始化(自身,父级): 超级(mygraphicscene,self)。\uuuu init\uuuuu() 自收根地刷(QBrush(QColor(50,50,50))) 类EllipseItem(QGraphicsSellipseitem): 定义初始化(自): 超级(EllipseItem,self)。\uuuu init\uuuuu() self.setAcceptHoverEvents(True) self.hover=False def绘制(自绘制、绘制、选项、小部件): painter.setRenderInts(qPaint.Antialiasing | qPaint.TextAntialiasing | qPaint.SmoothPixmapTransform | qPaint.HighQualityAntialiasing,True) 画师.镶嵌(QBrush(QColor(170255))) 如果self.isSelected(): 画师设置笔(QPen(QColor(255255255),2,Qt.SolidLine,Qt.RoundCap,Qt.RoundJoin)) 其他: 画师设置笔(QPen(QColor(30,30,30),2,Qt.SolidLine,Qt.RoundCap,Qt.RoundJoin)) self.setRect(-16,-16,16,16) 如果self.hover: 画师设置笔(QPen(QColor(255255255),2,Qt.SolidLine,Qt.RoundCap,Qt.RoundJoin)) self.setRect(-18,-18,18,18) painter.drawerlipse(self.rect()) def hoverEnterEvent(自我,事件): self.hover=True self.update() 超级(自我.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu def hoverLeaveEvent(自我,事件): self.hover=False self.update() 超级(自我.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu 类MyMainWindow(QMainWindow): 定义初始化(自): 超级(MyMainWindow,self)。\uu初始化 self.setWindowTitle(“测试”) 自我调整大小(800600) self.gv=MyGraphicsView() 自gv设置场景(MyGraphicscene(自)) lay_main=QVBoxLayout() lay_main.addWidget(self.gv) widget_main=QWidget() widget_main.setLayout(主布局) self.setCentralWidget(widget_main) self.populate() def填充(自我): scene=self.gv.scene() item=EllipseItem() item.setFlag(QGraphicsItem.ItemIs
    import sys
    from PySide.QtGui import *
    from PySide.QtCore import *
    import random
    
    
    class MyGraphicsView(QGraphicsView):
        def __init__(self):
            super(MyGraphicsView, self).__init__()
            self.setDragMode(QGraphicsView.RubberBandDrag)
            self._isPanning = False
            self._mousePressed = False
            # self.setBackgroundBrush(QImage("C:/Users/jmartini/Desktop/Temp/images/flag_0140.jpg"))
            self.setCacheMode(QGraphicsView.CacheBackground)
            self.setHorizontalScrollBarPolicy( Qt.ScrollBarAlwaysOff )
            self.setVerticalScrollBarPolicy( Qt.ScrollBarAlwaysOff )
    
    
        def mousePressEvent(self,  event):
            if event.button() == Qt.LeftButton:
                self._mousePressed = True
                if self._isPanning:
                    self.setCursor(Qt.ClosedHandCursor)
                    self._dragPos = event.pos()
                    event.accept()
                else:
                    super(MyGraphicsView, self).mousePressEvent(event)
            elif event.button() == Qt.MiddleButton:
                self._mousePressed = True
                self._isPanning = True
                self.setCursor(Qt.ClosedHandCursor)
                self._dragPos = event.pos()
                event.accept()
    
    
        def mouseMoveEvent(self, event):
            if self._mousePressed and self._isPanning:
                newPos = event.pos()
                diff = newPos - self._dragPos
                self._dragPos = newPos
                self.horizontalScrollBar().setValue(self.horizontalScrollBar().value() - diff.x())
                self.verticalScrollBar().setValue(self.verticalScrollBar().value() - diff.y())
                event.accept()
            else:
                super(MyGraphicsView, self).mouseMoveEvent(event)
    
    
        def mouseReleaseEvent(self, event):
            if event.button() == Qt.LeftButton:
                if self._isPanning:
                    self.setCursor(Qt.OpenHandCursor)
                else:
                    self._isPanning = False
                    self.setCursor(Qt.ArrowCursor)
                self._mousePressed = False
            elif event.button() == Qt.MiddleButton:
                self._isPanning = False
                self.setCursor(Qt.ArrowCursor)
                self._mousePressed = False
            super(MyGraphicsView, self).mouseReleaseEvent(event)
    
    
        def mouseDoubleClickEvent(self, event): 
            self.fitInView(self.sceneRect(), Qt.KeepAspectRatio)
            pass
    
    
        def keyPressEvent(self, event):
            if event.key() == Qt.Key_Space and not self._mousePressed:
                self._isPanning = True
                self.setCursor(Qt.OpenHandCursor)
            else:
                super(MyGraphicsView, self).keyPressEvent(event)
    
    
        def keyReleaseEvent(self, event):
            if event.key() == Qt.Key_Space:
                if not self._mousePressed:
                    self._isPanning = False
                    self.setCursor(Qt.ArrowCursor)
            else:
                super(MyGraphicsView, self).keyPressEvent(event)
    
    
        def wheelEvent(self,  event):
            # zoom factor
            factor = 1.25
    
            # Set Anchors
            self.setTransformationAnchor(QGraphicsView.NoAnchor)
            self.setResizeAnchor(QGraphicsView.NoAnchor)
    
            # Save the scene pos
            oldPos = self.mapToScene(event.pos())
    
            # Zoom
            if event.delta() < 0:
                factor = 1.0 / factor
            self.scale(factor, factor)
    
            # Get the new position
            newPos = self.mapToScene(event.pos())
    
            # Move scene to old position
            delta = newPos - oldPos
            self.translate(delta.x(), delta.y())
    
    
    class MyGraphicsScene(QGraphicsScene):
        def __init__(self,  parent):
            super(MyGraphicsScene,  self).__init__()
            self.setBackgroundBrush(QBrush(QColor(50,50,50)))
    
    
    class EllipseItem(QGraphicsEllipseItem):
        def __init__(self):
            super(EllipseItem,  self).__init__()
            self.setAcceptHoverEvents(True)
            self.hover = False
    
        def paint(self, painter, option, widget):
            painter.setRenderHints( QPainter.Antialiasing | QPainter.TextAntialiasing | QPainter.SmoothPixmapTransform | QPainter.HighQualityAntialiasing, True )
            painter.setBrush(QBrush(QColor(170,170,170,255)))
    
            if self.isSelected():
                painter.setPen(QPen(QColor(255,255,255), 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
            else:
                painter.setPen(QPen(QColor(30,30,30), 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
    
            self.setRect(-16, -16, 16, 16)
    
            if self.hover:
                painter.setPen(QPen(QColor(255,255,255), 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
                self.setRect(-18, -18, 18, 18)
    
            painter.drawEllipse(self.rect())
    
    
        def hoverEnterEvent(self, event):
            self.hover = True
            self.update()
            super(self.__class__, self).hoverEnterEvent(event)
    
        def hoverLeaveEvent(self, event):
            self.hover = False
            self.update()
            super(self.__class__, self).hoverEnterEvent(event)
    
    
    class MyMainWindow(QMainWindow):
        def __init__(self):
            super(MyMainWindow, self).__init__()
            self.setWindowTitle("Test")
            self.resize(800,600)
    
            self.gv = MyGraphicsView()
            self.gv.setScene(MyGraphicsScene(self))
    
            lay_main = QVBoxLayout()
            lay_main.addWidget(self.gv)
            widget_main = QWidget()
            widget_main.setLayout(lay_main)
            self.setCentralWidget(widget_main)
    
            self.populate()
    
    
        def populate(self):
            scene = self.gv.scene()
            item = EllipseItem()
            item.setFlag( QGraphicsItem.ItemIsSelectable )
            scene.addItem(item)
    
    
    def main():
        app = QApplication(sys.argv)
        ex = MyMainWindow()
        ex.show()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    
    def paint(self, painter, option, widget):
        painter.setRenderHints( QPainter.Antialiasing | QPainter.TextAntialiasing | QPainter.SmoothPixmapTransform | QPainter.HighQualityAntialiasing, True )
        painter.setBrush(QBrush(QColor(170,170,170,255)))
    
        # if self.isSelected():
        #     painter.setPen(QPen(QColor(255,255,255), 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
        # else:
        #     painter.setPen(QPen(QColor(30,30,30), 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
    
        #self.setRect(-16, -16, 16, 16)
        center = QPointF(0,0)
        if self.hover:
            painter.setPen(QPen(QColor(255,255,255), 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
            # self.setRect(-10,-10, 20, 20) # this one wont fit the ellipse
            self.setRect(-11, -11, 22, 22)
            # plus 2 
            painter.drawEllipse(center, 10, 10)
        else:
            painter.setPen(QPen(QColor(30,30,30), 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))  
            self.setRect(-8, -8, 16, 16)  # but this one does
            painter.drawEllipse(center, 8, 8)