PDA

View Full Version : multi-qgraphicsitem display help



mcarter
3rd April 2012, 19:53
Sorry about the general-purpose title, was not sure how to exactly sum this up in a simple one-liner

I am using QGraphicsView to display some pixmaps of data. Overlayed on top of these pixmaps are item data. Not sure if this matters, but there could be many. many of these items to display. This data can come from an outside source or from a user manually creating the item. The data will contain x,y positioning and width,height size information about the item to be displayed . . . so, a rectangular box. Internally the rect will be defined as -w/2.,0 w,h. Associated with this box will be another item, an icon, so to speak. I am ignoring transforms on the icon so I can draw the icon with a user defined size.

The problem that I am having is that I want the icon to appear above the upper left corner or the box, if the box is visible . . . and the box is visible unless the user zooms far enough out that that the box is too small (level of detail < X). If the box is not visible, I want the icon to move to the top-centered location . . . which should be the original x,y position.

7553

Currently I am using a item group to hold the rect item and the shape (icon) item. I cannot find a way to trigger the move (setPos) of the icon based on the level of detail determined inside the paint method of the rect item.

I was hoping that someone could point me in some direction to resolve this.
Anything would be appreciated

wayfaerer
4th April 2012, 07:25
I'm pretty certain you shouldn't be calling setPos inside the paint function, but rather you should be painting it differently, e.g., if it's big enough to see:


painter->drawRect(rect.translated(someAmount, 0); // translate it to the left

otherwise:


painter->drawRect(rect);

If you want more specific advice, it would be helpful to see your current paint function.

mcarter
5th April 2012, 13:40
The item group did not seem to work for me, so I moved to a parent-child relationship. The rect item is the parent with the icon item being the child. I seem to have better control of the icon since its "coordinate system" is based on the parent. I was wondering if it is okay to call setPos in the paint method of the parent to control the position of the child.


void MyRectItem::paint( QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget )
{
Q_UNUSED(widget);
Q_UNUSED(option);
qreal lod = option->levelOfDetailFromTransform(painter->worldTransform());
bool bShow = ( lod > 0.05 );
if( bShow )
{
painter->setPen( pen() );
painter->setBrush( brush() );
painter->drawRect( rect() );
}

if( bShow != m_bPainted && NULL != m_pIcon )
{
m_bPainted = bShow;
// this seems to work okay, but is it "legal" to make this setPos call?
m_pIcon->setPos( m_bPainted ? rect().topLeft() : QPointF( 0., 0. ) );
}
}

wysota
5th April 2012, 17:58
No, never do that.

mcarter
5th April 2012, 19:31
I am not happy with the "fix", but like I said, it seems to work . . . maybe is this simple isolated case.
So what is a/the better solution to move this child.

wysota
5th April 2012, 23:10
To be honest, it is hard for me to understand the problem you have. An obvious solution seems to me to make the icon item a child of the main item and make it ignore transformations. When the main item becomes small as a result of scaling it (or the view) then at some point the physical coordinates of the icon item should match the physical coordinates of (currently very small) main item without doing anything special.