【问题标题】:Making QGraphicsLineItem less fiddly to drag使 QGraphicsLineItem 不那么繁琐地拖动
【发布时间】:2019-04-04 08:10:21
【问题描述】:

如下所示,我创建了一个可拖动的 QGraphicsLineItem。但问题是选择线路非常繁琐。我想增加线条的“选择半径”,使其更容易拖动

from PySide import QtGui, QtCore
import sys

class VerticalLine(QtGui.QGraphicsLineItem):
    def __init__(self, x , y0 , y1 , parent=None):
        super(VerticalLine, self).__init__(x , y0 , x , y1 , parent)
        self.setFlag(QtGui.QGraphicsLineItem.ItemIsMovable)
        self.setFlag(QtGui.QGraphicsLineItem.ItemSendsGeometryChanges)
        self.setCursor(QtCore.Qt.SizeAllCursor) 


class Editor(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Editor, self).__init__(parent)

        line = VerticalLine( 10 , 10 , 100 )
        scene = QtGui.QGraphicsScene()
        scene.addItem( line )
        view = QtGui.QGraphicsView()
        view.setScene( scene )

        self.setGeometry( 250 , 250 , 600 , 600 )
        self.setCentralWidget(view)
        self.show()

if __name__=="__main__":
    app=QtGui.QApplication(sys.argv)
    myapp = Editor()
    sys.exit(app.exec_()) 

【问题讨论】:

    标签: python pyside qgraphicsview qgraphicsscene qgraphicslineitem


    【解决方案1】:

    您必须覆盖 shape() 和 boundingRect() 方法,以便它们返回更大的区域,并且为此使用 QPainterPathStroker 来创建一个使用线条作为基础并确定宽度的区域。

    class VerticalLine(QtGui.QGraphicsLineItem):
        def __init__(self, x , y0 , y1 , parent=None):
            super(VerticalLine, self).__init__(x , y0 , x , y1 , parent)
            self.setFlag(QtGui.QGraphicsLineItem.ItemIsMovable)
            self.setFlag(QtGui.QGraphicsLineItem.ItemSendsGeometryChanges)
            self.setCursor(QtCore.Qt.SizeAllCursor) 
    
        def shape(self):
            path = super(VerticalLine, self).shape()
            stroker = QtGui.QPainterPathStroker()
            stroker.setWidth(20)
            return stroker.createStroke(path)
    
        def boundingRect(self):
            return self.shape().boundingRect()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-29
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多