PDA

View Full Version : QGraphicsItem in QGraphicsScene - Painting items to x,y coordinates...



ikeurb
14th July 2009, 15:37
I am new to painting QGraphicsItems on a QGraphicsScene.

I am trying to paint a series of lines horizontally across a scene. I can't seem to get the lines to paint at the proper 'y' axis. No matter what 'y' coordinate I specify in the scene by explicitly setting the items position, the items appear in order from the very top left of the scene towards the bottom.

Changing the scenes alignment will change where the items paint, but I need to explicitly specifiy where the items go on the scene, ignoring the scene alignment.

I want to specify the item to paint at a specific point in the scene, but the items are always painted to the top left of the scene.

My code below:



QGraphicsScene *m_pqMainScene;
QGraphicsItem *m_paItem[MAX_NUMBER_OF_ITEMS];

int xPos = 0, yPos = 395;
int itemHeight = 15;
int horizontalLineLength = (numberOfItems() * itemHeight);

// Horizontal line
for (long lIndex = 1; lIndex <=numberOfFromItems(); lIndex++)
{
m_paItem[lIndex] = new QGraphicsLineItem(0, 0, horizontalLineLength, 0, 0);
m_paItem[lIndex]->setPos(xPos, yPos);
m_pqMainScene->addItem(m_paItem[lIndex]);
yPos -= itemHeight;
}



What am I doing wrong???

Thanks!

wysota
14th July 2009, 23:02
How exactly would you like them to appear? The code you pasted should paint a series of horizontal lines one over another and the lines should probably start at the top left corner of the scene and descend downwards until they reach the vertical position of 395.

ikeurb
15th July 2009, 13:42
I'd like the lines to paint starting at the bottom of the scene then up towards the top.

I am going to eventually do the same thing with vertical lines to form a grid starting from the bottom left.

wysota
15th July 2009, 13:56
So what is the difference between what you obtain and what you would like to obtain?

ikeurb
15th July 2009, 17:42
The lines paint from the top to bottom, when I'd like them to paint from bottom to top.

But, no matter what I enter as the 'y' coordinate, the items still paint at the very top of the scene.

wysota
15th July 2009, 20:33
Shouldn't it be something like:


yPos = sceneRect().bottom(); // instead of 395?

ikeurb
16th July 2009, 16:35
I ended up using int yPos = height();

But, I created my own class to paint the lines, subclassed from QGraphicsItem where I pass in the x and y starting position, line length.



// Horizontal line
for (long lIndex = 1; lIndex <=numberOfFromItems(); lIndex++)
{
m_paItem[lIndex ] = new AGridLine(xPos, yPos, horizontalLineLength, yPos);
m_pScene->addItem(m_paItem[lIndex]);
yPos -= padding;
}

JovianGhost
21st March 2010, 03:11
I saw this old thread, and thought I'd add a suggestion, since I encountered something similar.
What about scaling the view to flip everything upside-down?



m_graphicsView.scale(1, -1);


This will effectively flip the view of the scene so that incrementing values of Y go from bottom to top, instead of top to bottom.
This assumes, of course, you're using a QGraphicsView in the first place.