QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout)
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.
Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout
Quote:
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?
Quote:
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.
Quote:
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
Code:
int main(int argc, char *argv[])
{
QGraphicsWidget * widget = new QGraphicsWidget;
QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
for(int i = 0; i < 10; i++)
{
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();
}
Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout
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.
1 Attachment(s)
Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout
Well, here is the basic example of how it looks the test code: Attachment 9739
I use QLabel but with setText() and using HTML code, so I can display both Pixmap and Text :)
Cheers.
Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout
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.
Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout
Set QGraphicsItem::ItemIsSelectable flag for the QGraphicsWidget (subclassed QLabel). Note that you will have to draw the selection rectangle explicitly.
Re: QGraphicsViews: how to arrange items using QGraphicsLinearLayout (or other layout
Yeah, already applied that flag (Python):
Quote:
graphicItem.setFlags(QGraphicsItem.ItemIsFocusable | QGraphicsItem.ItemIsSelectable)
And for the subclassed QLabel, for focus i've added:
Quote:
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?