QT开发中文网

在graphicsview中使用定时器每隔3s创建一个graphicsrectitem,并慢慢做直线运动

您可以在GraphicsView中使用定时器来创建GraphicsRectItem并实现直线运动。下面是一个示例代码:

”`python from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsRectItem, QApplication from PyQt5.QtCore import QTimer, QPointF

class CustomRectItem(QGraphicsRectItem):

def __init__(self):
    super().__init__()

    self.setRect(0, 0, 50, 50)  # 设置矩形大小和位置

    self.start_pos = QPointF(0, 0)  # 起始位置
    self.end_pos = QPointF(200, 200)  # 终点位置

    self.current_pos = self.start_pos  # 当前位置

def updatePosition(self):
    delta_x = (self.end_pos.x() - self.start_pos.x()) / 100.0
    delta_y = (self.end_pos.y() - self.start_pos.y()) / 100.0

    self.current_pos.setX(self.current_pos.x() + delta_x)
    self.current_pos.setY(self.current_pos.y() + delta_y)

    if abs(self.current_pos.x() - self.end_pos.x()) < abs(delta_x):
        if abs(self.current_pos.y() - self.end_pos.y()) < abs(delta_y):
            # 达到终点位置后停止移动
            return False

    self.setPos(self.current_pos)

    return True

class GraphicsView(QGraphicsView):

def __init__(self):
    super().__init__()

    scene = QGraphicsScene()
  <br/><br/><br/><font size=1 color=#CCCCCC>内容由零声教学AI助手提供,问题来源于学员提问</font>

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=5848

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?