Results 1 to 9 of 9

Thread: How to make some items non movable on scene

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to make some items non movable on scene

    This is the layout of my dataplot widget:

    Dataplot.png

    The DataplotWidget is derived from QWidget, and uses a QLabel for the title, up to 4 QwtScaleWidget instances as top / bottom / left / right axes, and another custom widget derived from QGraphicsView as the "canvas" where the data is displayed.

    The DataplotWidget's showEvent() and resizeEvent() each call a protected method named calculateLayout(). The purpose of this method is to eventually calculate the size and position of the canvas by subtracting out the areas occupied by the other widgets in the Dataplot:


    1. Start with the full DataplotWidget rect
    2. If the title is visible, set its position to the top left and set the top of the canvas rect to the bottom of the title rect
    3. If the top axis is visible, set its top to the bottom of the title. Set the top of the canvas to the bottom of the axis
    4. If the bottom axis is visible, set the bottom of the canvas rect to the top of the bottom axis rect
    5. If the left axis is visible, set its top to the top of the canvas and bottom to the bottom of the canvas. Move the left side of the canvas to the right side of the left axis
    6. If the right axis is visible, set its top and bottom to the canvas top and bottom. Move the right side of the canvas to the left side of the axis
    7. if the top and/or bottom axes are visible, adjust their left and right sides to match the canvas left and right.



    The canvas uses an overlay widget to implement zooming. When the left mouse button is clicked in the canvas, the overlay widget is displayed at the same position and size as the canvas widget, and is used to draw the rubberband. If your canvas contains a very complex scene, then it is best to take a snapshot of the contents (i.e. render the graphics view to a pixmap) and use the pixmap as the background of the overlay window. This allows you to smoothly move the rubberband around without triggering paint events in the graphics view. When the mouse is released, the overlay window is hidden and the canvas widget emits a "zoomed( const QRectF & )" signal, where the coordinates of the QRect are in scene coordinates.

    The DataplotWidget is connected to this signal. When it receives it, it adjusts the axis ranges as appropriate. I have implemented zooming only for the left and bottom axes, but you could extend the behaviour by supporting zoom on any orthogonal pair of axes. If I ever need that, I will have to go back and engineer it in.

    Since each of the sub-widgets is derived from QWidget, they all take care of their own painting. The DataplotWidget itself does not have a paintEvent handler. The canvas uses an external QGraphicsScene, but has its own viewport. This allows the same scene to be displayed on more than one dataplot, with different viewports or level of zoom, and more importantly, without duplicating the underlying data. If you do not need to do this, then you can simply a bit by making the scene a member of the dataplot widget.

    Hope this helps a bit. The actual dataplot I have implemented is much more complex than this. The mouse interactions are implemented in a separate class based on QStateMachine, because it supports about 10 different rubberband modes (horizontal line, vertical line, rectangle, horizontal box, vertical box, lasso, etc.) for zooming, panning, range selection, object selection, and so forth. There is a zoom stack that allows the user to zoom in and then pop out to the previous zoom, etc.

  2. The following user says thank you to d_stranz for this useful post:

    tarunrajsingh (16th April 2013)

Similar Threads

  1. Replies: 0
    Last Post: 14th February 2012, 11:03
  2. Moving items in a scene via slots
    By ventura8 in forum Qt Programming
    Replies: 2
    Last Post: 30th August 2011, 15:16
  3. add items into scene
    By Noxxik in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2009, 16:32
  4. Can't move Items in the scene
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2008, 09:40
  5. Cann't move movable items on custom graphics view
    By wojtekw in forum Qt Programming
    Replies: 2
    Last Post: 3rd March 2008, 21:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.