使用Qt模型/视图框架在另一个视图中进行用户编辑的视图中通知QGraphicsItem [英] Using Qt model/view framework to notify QGraphicsItem in a view of user-edit made in another view

查看:227
本文介绍了使用Qt模型/视图框架在另一个视图中进行用户编辑的视图中通知QGraphicsItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设计一个多视图应用程序以使用Qt的模型/视图框架,以便可以将很多复杂的逻辑移出我的QGraphicsItem类.

I am trying to design a multiview application to use Qt's model/view framework so that I can move lots of complex logic out of my QGraphicsItem classes.

如下图所示,该应用程序由两个(或多个)视图组成,每个视图在相同的水平位置包含一系列相同的红色垂直引导线A1B1C1 ...沿每个视图.

As shown in the figure below, the application consists of two (or more) views each containing of an identical series of red vertical guide lines A1 , B1, C1 ... at the same horizontal positions along each view.

我想确保当用户拖动一个指导线视图时,例如从上图所示的点A1A_1'拖动时,其他视图中的所有相应指导线都以相同的方向移动距离,并且方向相同,例如从A2A2'.同样,不可能将一条指导线拖到另一条指导线上,即,指导线必须保持其相对顺序.这是我想从GuideLine类移入模型的逻辑的一部分.

I'd like to ensure that when the user drags a guide line one view, say from a point A1 to A_1' as shown in the figure above, all corresponding guide lines in the other views move by the same distance and in the same direction, for example from A2 to A2'. Also it should not be possible to drag one guide line past another i.e. guide lines must retain their relative orders. This is part of the logic that I would like to move out of my GuideLine class, into the model.

我知道拖动参考线会触发QGraphicsItem :: itemChange,如下面的代码片段所示.我不知道如何最好

I know dragging guide lines triggers QGraphicsItem::itemChange, as shown in the snippet below. What I don't know is how best to

  1. 将候选值转发给模型以进行验证(和存储).我知道该视图是接口用于通过信号和插槽机制与模型进行互操作的标准接口".如我所见,最大的问题是QGraphicsItem::itemChange必须立即返回经过验证的值,而该值不能依赖异步信号时隙机制.
  2. 在其他视图中通知准则更改,而不会触发创建通知的级联,例如,A1B1不断通知彼此.
  1. Forward the candidate value to the model for validation (and storage). I am aware that the view is the interface "standard interface for interoperating with models through the signals and slots mechanism". The big problem, as I see it, is that QGraphicsItem::itemChange must immediately return validated value it cannot rely on the asynchronous signal-slot mechanism.
  2. Notify the guide lines in the other views of the change without triggering creating a cascade of notifications, for example where A1 and B1 endlessly notify each other.

-

class GuideLine( QtGui.QGraphicsLineItem ):
    ...
    # Called when item is about to change to value
    def itemChange( self , change , value ):
        # TODO 
        #  1. validate value in model
        #  2. if change is valid notify peers in other views
        pass

推荐答案

QGraphicsScene和QGraphicsView可以看作是模型/视图实现,其中场景是模型,而QGraphicsView是视图.

QGraphicsScene and QGraphicsView could be seen as a model/view implementation where the scene would be the model and the QGraphicsView would be the view.

两个QGraphicsView可以共享同一场景,当您从一个视图修改场景时,第二个视图也将被更新.

Two QGraphicsView can share the same scene and when you modify the scene from one view, the second one will be also updated.

一个简单的例子:

if __name__ == "__main__":

    app = QApplication([])
    scene = QGraphicsScene(0, 0, 1000, 1000)
    view1 = QGraphicsView()
    view2 = QGraphicsView()

    item = QGraphicsRectItem(QRectF(0, 0, 40, 40))
    item.setFlag(QGraphicsItem.ItemIsMovable)
    scene.addItem(item)

    view1.setScene(scene)
    view2.setScene(scene)

    w = QWidget()
    layout = QVBoxLayout(w)
    layout.addWidget(view1)
    layout.addWidget(view2)
    w.show()

    sys.exit(app.exec_())

rect项目是可移动的,并且将同时在两个视图中移动.

The rect item is movable and will move in the two views at the same time.

这篇关于使用Qt模型/视图框架在另一个视图中进行用户编辑的视图中通知QGraphicsItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆