[PyQt] Looking for a sample drawling tool in PyQt4

Simon Hibbs simon.hibbs at gmail.com
Fri Jan 1 13:32:02 GMT 2010


I think DiagramScene is the best place to start, I used it as the
starting point for my own application, developing my own objects and
menus in parallel with the existing ones until finally I was able to
delete all the old code.

The existing code for creating objects should be fine for you, with
gradual amendments to get the functionality you want. You'd have to
implement item resizing yourself. For example, duplicating the square
item and replacing the code that draws the square with code using
variables for the lengths of sides instead of fixed values.

For my application I created a new parent item object sub-classed from
QGraphicsItemGroup so that I could add text and other annotations to
it as child items, but still treat it all as a single item for most
purposes. You will probably need to do that when you add the labels.
So in my scheme I have a QGraphicsItemGroup object and it's actual
drawn shape is a child QGraphicsPolygonItem and associated text is a
QGraphicsSimpleTextItem.

    def displayGridCoordText(self, bool_flag):
        for item in self.items():
            if isinstance(item, MapCell):
                item.displayGridCoordText(bool_flag)

For snap-to-grid I don't think there is any built-in functionality,
you'd just need to override the methods that set an object's position
to check and adjust the new position before setting it.

Simon Hibbs


More information about the PyQt mailing list