PDA

View Full Version : QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout)



GuS
29th October 2013, 21:43
Hi!

I'm developing a class that must contain like "products for sale list" in the form of graphics (QPixmap). So, I've chose QGraphicsView (for a similiar situation, in the past I've used QScrollArea and a QGridLayout to manage widgets buts is slow when you use to many widgets). The problem is that I've never used it.

What I plan to do is: having a way to arrange QPixmaps items inside QGraphicsView, so my question is: Do I need a layout to handle pixmaps in a easy way? Having in mind that items can be added and deleted so I must re-arrange items.

I've reading how QGraphicsViews works, the scene and such, but I don't have clear how to work with a layout and pixmaps items inside this.

Can anyone supply a small example code?

I will be very grateful.

Thanks in advance.

Santosh Reddy
30th October 2013, 14:03
I'm developing a class that must contain like "products for sale list" in the form of graphics (QPixmap). So, I've chose QGraphicsView (for a similiar situation, in the past I've used QScrollArea and a QGridLayout to manage widgets buts is slow when you use to many widgets).
How many widgets are there?


What I plan to do is: having a way to arrange QPixmaps items inside QGraphicsView, so my question is: Do I need a layout to handle pixmaps in a easy way? Having in mind that items can be added and deleted so I must re-arrange items.
One easy way is to use QGraphicsWidget and QGraphicsLayout. If you want to use QGraphicsPixmapItem then you have take care to layout yourself.


I've reading how QGraphicsViews works, the scene and such, but I don't have clear how to work with a layout and pixmaps items inside this.

Can anyone supply a small example code?
This is an example to use QGraphicsWidget


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView view;
QGraphicsScene scene;
QGraphicsWidget * widget = new QGraphicsWidget;
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);

for(int i = 0; i < 10; i++)
{
QLabel * label = new QLabel;
label->setPixmap(QPixmap("C:/image.jpg"));
QGraphicsWidget * w = scene.addWidget(label);
layout->addItem(w);
}

widget->setLayout(layout);
scene.addItem(widget);
view.setScene(&scene);
view.show();

return a.exec();
}

GuS
30th October 2013, 15:16
Thanks for reply! yesterdays after reading, which you said the same, in some forums i got the conclusion about QGraphicsPixmapItem about the layout. So, I use QLabel to achieve this, plus using QGraphicsLinearLayout :)

Thanks!, now I will test things over this :)

Cheers.

GuS
30th October 2013, 17:21
Well, here is the basic example of how it looks the test code: 9739

I use QLabel but with setText() and using HTML code, so I can display both Pixmap and Text :)

Cheers.

GuS
30th October 2013, 20:50
Santosh Reddy: I have another dub: in this example I've subclassed QLabel, to implemente a focus "effect" (by using focusInEvent and focusOutEvent event, and stylesheet to change border color when selected). Now, how i do select multiple items/widgets in QGraphicsView?

Thanks.

Santosh Reddy
31st October 2013, 02:18
Set QGraphicsItem::ItemIsSelectable flag for the QGraphicsWidget (subclassed QLabel). Note that you will have to draw the selection rectangle explicitly.

GuS
31st October 2013, 10:57
Yeah, already applied that flag (Python):



graphicItem.setFlags(QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemIsSelectable)


And for the subclassed QLabel, for focus i've added:



def focusInEvent(self, event):
self.focusState.emit(True)
self.setStyleSheet("QLabel{background-color: transparent; border: 2px solid #90b6e2; border-radius: 5px;}")
return QLabel.focusInEvent(self, event)


def focusOutEvent(self, event):
self.focusState.emit(False)
self.setStyleSheet("QLabel{background-color: transparent; border: 2px solid transparent; border-radius: 5px;}")
return QLabel.focusInEvent(self, event)


This last one, would be a problem for selecting more than one QLabel/widget at time? or is something that i must handle from QGraphicsView itself?