使用QRectF

问题描述

我有一个QGraphicsScene,其中添加了锚定到QWidget对象的QRectF对象以移动它们。我需要从QRectF捕获事件或信号,但是mousepressEvent方法永远不会运行。 这些对象具有某种平衡,并且很难用QRect或QGraphicsRectItem替换QRectF,因为仅在场景中绘制基本矩形才接受该类。

我也尝试实现mousepressEvent方法为GraphicBlock类(它是QWidget),但是什么也没发生。

这是我的QRectF

class BlockRect(QRectF):
def __init__(self,x,y,dim1,dim2,block_type):
    super(QRectF,self).__init__(x,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,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)

    # Creation of the graphic block
    block = GraphicBlock(self.numBlocks,block_type,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,它具有事件方法,但不执行它们。我认为我应该控制QGraphicsScene对象中的事件。

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

def __init__(self,block_id,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,0)
            count += 1
    else:
        type_label.setStyleSheet(ZERO_PARS_BLOCK_TITLE)

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)