PDA

View Full Version : Item size not updating after scaling QGraphicsView



Santa.fat
26th October 2015, 07:02
i want to show some images vertically using QGraphicsView and QLabel in pyqt.

i am using scale method for scaling QGraphicsView. the problem is that when i scale (zoom in) QGraphicsView size of labels not updating and because of that when i scale QGraphicsView and setting label's pixmap again resulting image quality is poor.

here is my code:


my_graphics_view = QtGui.QGraphicsView()
scene = QtGui.QGraphicsScene()
graphics_widget = QtGui.QGraphicsWidget()
layout = QtGui.QGraphicsLinearLayout(QtCore.Qt.Vertical)
default_size = QtCore.QSize(400 , 400)
for number in xrange(num_pages):
lbl = QtGui.QLabel()
lbl.setMinimumSize(default_size)
lbl.setStyleSheet("background-color : white")
lbl.setContentsMargins(0, 0, 0, 0)
lbl.setScaledContents(True)
widget = scene.addWidget(lbl)
layout.addItem(widget)
graphics_widget.setLayout(layout)
scene.addItem(graphics_widget)
my_graphics_view.setScene(scene)
my_graphics_view.show()

How can I get proper size for labels or make them resize automatically?

anda_skoa
26th October 2015, 08:23
Any reason you are using a label and not a QGraphicsPixmapItem?

Cheers,
_

Santa.fat
26th October 2015, 09:04
I want using a white background for empty pages because sometimes I want to add static number of items with same size to QGraphicsView and only some of them have pixmaps and sometimes pixmaps changed

anda_skoa
26th October 2015, 09:17
So?

Sounds like a QGraphicsPixmapItem on top of a QGraphicsRetangleItem.
Or a custom item that paints a background and then the image

Cheers,
_

Santa.fat
26th October 2015, 10:10
Could you please give me a pseudo code or some example?

anda_skoa
26th October 2015, 11:16
The graphics item framework allows you to create new items by either aggregating existing items or by implementing your own items directly.

For the first you could, for example, group a rectangle item and a pixmap item in a graphics item group, or using the rectangle item as the parent of the pixmap item.
For the latter, there is a simple example in the documentation of QGraphicsItem, basically providing a bounding rectangle and some custom painting.

The examples provided by in Qt's documentation have occurences of both approaches http://doc.qt.io/qt-5/examples-graphicsview.html

Cheers,
_

Santa.fat
26th October 2015, 14:41
thank you anda_skoa but i didn't see any example for QGraphicsPixmapItem on top of a QGraphicsRetangleItem.

anda_skoa
26th October 2015, 16:05
I am sorry, did you expect that any of the examples would be miraculously have exactly the combined item you wanted instead of showing generically useful techniques?

Cheers,
_

Santa.fat
26th October 2015, 16:36
:o no but i do not know know how to combine QGraphicsPixmapItem and QGraphicsRetangleItem. any example or pseudo code of how can i fix my problem would be helpful.

anda_skoa
26th October 2015, 19:21
Either by using a QGraphicsItemGroup (as already mentioned in comment #6) or by having both as children of a common parent, or by using the rectangle as the parent of the pixmap (as also already mentioned in comment #6)

Cheers,
_

Santa.fat
26th October 2015, 20:12
dear anda_skoa this is my first time using QGraphicsView and i am a little bit confused. could you please give me pseudo code even in 5 lines of code.

thank you

Santa.fat
27th October 2015, 20:38
hi.

could you please give me an example of how to arrange QGraphicsPixmapItem in QGraphicsScene vertically?

anda_skoa
28th October 2015, 07:42
Using a rect item as a parent of a pixmap item


QGraphicsItem *rect = new QGraphicsRectItem();
QGraphicsItem *pixmap = new QGraphicsPixmapItem(rect);

or with aggregation


class MyItem : public QGraphicsRectItem
{
QGraphicsPixmapItem *m_pixmap;
public:
MyItem(QGraphicsItem *parent = 0) : QGraphicsRectItem(parent)
{
m_pixmap = new QGraphicsPixmapItem(this);
}
}

or, as said earlier, using QGraphicsItemGroup

Cheers,
_

Santa.fat
29th October 2015, 17:31
thank you.

can i set a fixed size for items? becuase i want set a fixed size for all of my items and then fill theme with pixmap.
another question is that how can i get the size of item after scaling?