PDA

View Full Version : Issue in placing the QGraphicsItems into QGraphicsLayout



aditya.kaole
8th September 2010, 08:46
I am using QGraphicsGridLayout in GraphicScene(class publicly inherited from QGraphicsScene)..

I have to place a QPushButton in column 1 of the QGraphicsGridLayout in all the rows. And want to place a QGraphicsItem into column 2 of each row..

I am able to see the two QPushButton one below another (after adding them in the QGraphicsGridLayout), but when i try to add the QGraphicsItem (GraphicsItem). they are placed at zero location of the scene.. And are not placed in the particular grid column location.

I don't want use the hard coded location (using setpos() ) for the QGraphicsItem. How can i do that?

Code snap shot:
================================
GraphicScene::GraphicScene(QWidget *parent)
{
setItemIndexMethod(QGraphicsScene::NoIndex);
setSceneRect(0, 0, 800, 1200);

graphics_scene_layout = new QGraphicsGridLayout();

QGraphicsProxyWidget *proxy_p_button_0 = new QGraphicsProxyWidget();
QGraphicsProxyWidget *proxy_p_button_1 = new QGraphicsProxyWidget();
QGraphicsProxyWidget *proxy_graphicsitem_0 = new QGraphicsProxyWidget();
QGraphicsProxyWidget *proxy_graphicsitem_1 = new QGraphicsProxyWidget();

QPushButton *p_button_0 = new QPushButton("-");
p_button_0->resize(20,20);

QPushButton *p_button_1 = new QPushButton("-");
p_button_1->resize(20,20);

proxy_p_button_0->setWidget(p_button_0);
proxy_p_button_1->setWidget(p_button_1);

p_button_0->show();
p_button_1->show();

graphics_scene_layout->addItem(proxy_p_button_0, 0, 0, Qt::AlignLeft | Qt::AlignTop);
graphics_scene_layout->addItem(proxy_p_button_1, 1, 0, Qt::AlignLeft | Qt::AlignTop);

GraphicsItem *item0 = new GraphicsItem(parent);
GraphicsItem *item1 = new GraphicsItem(parent);

proxy_graphicsitem_0->setGraphicsItem(item0);
proxy_graphicsitem_1->setGraphicsItem(item1);

graphics_scene_layout->addItem(proxy_graphicsitem_0, 0, 1);
graphics_scene_layout->addItem(proxy_graphicsitem_1, 1, 1);

QGraphicsWidget *form = new QGraphicsWidget;
form->setLayout(graphics_scene_layout);
this->addItem(form);
}

================================


Am i missing some thing?? I was expecting placing the item in correct gird row/column will take care of position in scene.

Thanks in Advance!

tbscope
8th September 2010, 08:58
I think you need to create a graphics layout item subclass and implement the setGraphicsItem there.

aditya.kaole
8th September 2010, 11:03
Sorry i could not understand exactly what you meant to say..

Shall i create GraphicsItem by inheriting QGraphicsLayoutItem instead of QGraphicsItem?
If Yes, I've already tried it.

The GraphicsItem, created is a customized QGraphicsItem created by me and A text has to be set into it. If i use the QGraphicsLayoutItem instead of QGraphicsItem.
This is customizations are not possible.

Also, if the QPushbutton are placed automatically at proper location in layout.
Whats is the problem with QGraphicsItem??

I also want to ask, can i insert another QGraphicsScene in the QGraphicsGridLayout?

tbscope
8th September 2010, 11:16
A layout in Qt does not directly contain any widgets or, in this case, graphics items.
You need to put the widget or graphics item in a container, which is called a layout item.

The layout only uses layout items or other layouts to do the actual work.

This means that you can leave your custom item exactly as it is right now.
What you do need to create is a custom layout item. This layout item will contain your custom graphics item and report to the acual layout the size of your custom item, among other things.



+------------------------------------------------------------+
| Layout |
| |
| +-----------------------------------------------+ |
| | Layout Item | |
| | | |
| | +--------------------+ | |
| | | Graphics Item | | |
| | +--------------------+ | |
| +-----------------------------------------------+ |
| |
| +-----------------------------------------------+ |
| | Layout Item | |
| | | |
| | +--------------------+ | |
| | | Graphics Item | | |
| | +--------------------+ | |
| +-----------------------------------------------+ |
| |
+------------------------------------------------------------+

aditya.kaole
9th September 2010, 07:33
Thank you very much for the help. Will do the necessary changes.