PDA

View Full Version : QGraphics scene position



junix
13th April 2012, 22:11
Hi people, :)

I'd like to set the scene itens atleast 10 points from the top and left GraphicsView border.
But I couldn't until now.

7584

A piece of my PyQt code:

(...)
picture = "ui/snap/" + gamepicture
if not os.path.isfile(picture):
picture = "ui/pixmaps/nopic.png"
if gamestatus:
color = QtGui.QColor(0, 0, 0, 0)
pix = QtGui.QPixmap(QtCore.QSize(238, 174))
pix.fill(color)
rect = QtCore.QRectF(0.0, 0.0, 238, 174)
painter = QtGui.QPainter()
painter.begin(pix)
painter.setRenderHints(QtGui.QPainter.Antialiasing , True)
path = QtGui.QPainterPath()
path.addRoundedRect(rect, 10.0, 10.0)
painter.drawPath(path)
brush = QtGui.QBrush()
brush.setTexture(QtGui.QPixmap(picture))
painter.fillPath(path, brush)
painter.end()
pixItem = GameItem(gameid, pix, None, self.scene)
pixItem.setPos((260 * col), lin)
col += 1
if col >= 6:
lin += 220
col = 0
self.graphicsView.setScene(self.scene)
self.graphicsView.setAlignment(QtCore.Qt.AlignLeft )
(...)

Some tip?

BR,

Junix

Talei
14th April 2012, 21:12
That's probably because You don't take into account initial shift/offset of items from the border.
I don't see initial values of variables but try something like this:


pixItem.setPos((260 * col) + offsetX, lin + offsetY)

junix
16th April 2012, 14:33
Sorry, but it doesn't work.
I've set the offset, but nothing change.

wysota
16th April 2012, 15:34
Did you set the scene size? And do you mean the initial position or that you always want to have 10 pixels of margin between the widget and its contents?

junix
16th April 2012, 16:04
I've set the size: rect = QtCore.QRectF(0.0, 0.0, 238, 174)
I have rounded corners: path.addRoundedRect(rect, 10.0, 10.0)

I'd like to separate from the borders (space of 10 pixels).

wysota
16th April 2012, 16:27
Ok, that's easy then. You need to use setViewportMargins() to make a margin between the widget and its viewport. It is possible QGraphicsView will try to override that in which case you'll need to override resizeEvent() and force those margins again on each resize. You will probably also want to change the frame of the viewport and possibly the frame of the scroll area itself.

junix
17th April 2012, 03:41
Perfect!

Thank you.

BR,

Junix