用QRectF捕捉事件

2024-06-02 04:35:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个QGraphicscene,我在其中添加定位到QWidget对象的QRectF对象,以便移动它们。我需要从QRectF捕获事件或信号,但mousePressEvent方法从未运行。 这些对象具有某种平衡,很难用QRect或QGraphicsRectItem替换QRectF,因为在场景中绘制基本rect只接受该类

我还尝试实现MousePresseEvent方法是GraphicBlock类(这是一个QWidget),但什么也没发生

这是我的简历

class BlockRect(QRectF):
def __init__(self, x, y, dim1, dim2, block_type):
    super(QRectF, self).__init__(x, y, dim1, dim2)
    self.block_type = block_type

def contains(self, point):
    if self.x() + self.width() \
       > point.x() > self.x() - self.width()/2:
        if self.y() + self.height() \
           > point.y() > self.y() - self.height()/2:
            return True
    return False

# Never called
def mousePressEvent(self, event):
    print("click!")
    dialog = MyDialog(self.block_type)
    dialog.exec()
    super(BlockRect, self).mouseDoubleClickEvent(event)

这就是我画它的方法:

    def draw_block(self, block_type):
    """Drawing a graphic clock with its properties"""

    # Setting the visible scene
    viewport_rect = QRect(0, 0, self.view.viewport().width(),
                          self.view.viewport().height())
    viewport = self.view.mapToScene(viewport_rect).boundingRect()
    start_x = viewport.x()
    start_y = viewport.y()

    # The initial point of each block is translated of 20px in order not to
    # overlap them (always in the visible area)
    point = QPoint(start_x + 20*(self.numBlocks % 20) + 20,
                   start_y + 20*(self.numBlocks % 20) + 20)
    transparent = QColor(0, 0, 0, 0)

    # Creation of the graphic block
    block = GraphicBlock(self.numBlocks, block_type, 0, self.scene)

    # Positioning the block
    proxy = self.scene.addWidget(block)
    proxy.setPos(point.x(), point.y())

    # Creation of the rect that will be parent of the QWidget GraphicBlock
    # in order to move it in the QGraphicsScene
    rect = BlockRect(point.x() + 10, point.y() + 10,
                     block.width() - 20, block.height() - 20,
                     block_type)

    # The rect is added to the scene and becomes the block's parent
    proxy_control = self.scene.addRect(rect, QPen(transparent), QBrush(transparent))
    proxy_control.setFlag(QGraphicsItem.ItemIsMovable, True)
    proxy_control.setFlag(QGraphicsItem.ItemIsSelectable, True)
    proxy.setParentItem(proxy_control)
    block.set_rect(rect)

    self.blocks[self.numBlocks] = block
    self.numBlocks += 1
    self.update()

我真的不知道或不理解我如何以某种方式捕捉事件

这里是我的QWidget类,即GraphicBlock,它有事件方法,但不执行它们。我想我应该控制QGraphicscene对象中的事件

class GraphicBlock(QWidget):
"""QWidget that carries both graphical and logical information about the
layer node
"""

def __init__(self, block_id, block_type, block_data, scene):
    super(GraphicBlock, self).__init__()
    self.block_id = block_id
    self.block_type = block_type
    self.block_data = block_data   # Just to try
    self.scene = scene
    self.rect = None

    # Setting style and transparent background for the rounded corners
    self.setAttribute(Qt.WA_TranslucentBackground)
    self.setStyleSheet(GRAPHIC_BLOCK_STYLE)

    # Block title label
    type_label = QLabel(block_type.name)
    type_label.setStyleSheet(BLOCK_TITLE_STYLE)

    # Main vertical layout: it contains the label title and grid
    layout = QVBoxLayout()
    layout.setSpacing(0)
    layout.addWidget(type_label)
    self.setLayout(layout)

    if block_type.parameters:
        # Creating the grid for parameters
        grid = QWidget()
        grid_layout = QGridLayout()
        grid.setLayout(grid_layout)
        layout.addWidget(grid)

        # Iterating and displaying parameters
        par_labels = dict()
        count = 1
        for par in block_type.parameters:
            par_labels[par] = QLabel(par)
            par_labels[par].setAlignment(Qt.AlignLeft)
            par_labels[par].setStyleSheet(PAR_BLOCK_STYLE)

            dim = QLabel("<dim>")
            dim.setAlignment(Qt.AlignRight)
            dim.setStyleSheet(DIM_BLOCK_STYLE)

            grid_layout.addWidget(par_labels[par], count, 1)
            grid_layout.addWidget(dim, count, 0)
            count += 1
    else:
        type_label.setStyleSheet(ZERO_PARS_BLOCK_TITLE)

def set_rect(自身,rect): self.rect=rect