Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should look into the QGraphicsView instead of what you are doing, it has this all built in already.</p> <p><a href="http://doc.qt.nokia.com/4.7-snapshot/qgraphicsview.html" rel="nofollow">http://doc.qt.nokia.com/4.7-snapshot/qgraphicsview.html</a></p> <p><a href="http://doc.qt.nokia.com/4.7-snapshot/qgraphicsscene.html" rel="nofollow">http://doc.qt.nokia.com/4.7-snapshot/qgraphicsscene.html</a></p> <pre><code>from PyQt4 import QtGui, QtCore class MyFrame(QtGui.QGraphicsView): def __init__( self, parent = None ): super(MyFrame, self).__init__(parent) self.setScene(QtGui.QGraphicsScene()) # add some items x = 0 y = 0 w = 45 h = 45 pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.green)) brush = QtGui.QBrush(pen.color().darker(150)) item = self.scene().addEllipse(x, y, w, h, pen, brush) item.setFlag(QtGui.QGraphicsItem.ItemIsMovable) if ( __name__ == '__main__' ): app = QtGui.QApplication([]) f = MyFrame() f.show() app.exec_() </code></pre> <p><strong>Edit: Showing how to create a QPainterPath</strong></p> <pre><code>from PyQt4 import QtGui, QtCore class MyFrame(QtGui.QGraphicsView): def __init__( self, parent = None ): super(MyFrame, self).__init__(parent) scene = QtGui.QGraphicsScene() self.setScene(scene) # add some items x = 0 y = 0 w = 45 h = 45 pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.green)) brush = QtGui.QBrush(pen.color().darker(150)) item = scene.addEllipse(x, y, w, h, pen, brush) item.setFlag(QtGui.QGraphicsItem.ItemIsMovable) # create an open path path = QtGui.QPainterPath() path.moveTo(-w, -h) path.lineTo(-w, h) path.lineTo(w, h) path.lineTo(w, -h) clr = QtGui.QColor('blue') clr.setAlpha(120) brush = QtGui.QBrush(clr) pen = QtGui.QPen(QtCore.Qt.NoPen) fill_item = scene.addRect(-w, y, w*2, h, pen, brush) path_item = scene.addPath(path) if ( __name__ == '__main__' ): app = QtGui.QApplication([]) f = MyFrame() f.show() app.exec_() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload