Thursday, 15 July 2010

c++ - position of an QGraphicsItem in scene always null -



c++ - position of an QGraphicsItem in scene always null -

for testing purposes, trying draw several rectangle on scene , add together text inside. rectangle supposes displayed in 1 column do, problem text don't. text stacked on top left of scene. futhermore, pos() , scenepos() homecoming (0,0) every boxes , texts.

here code responsible that. can tells i'm doing wrong?

qhash<qstring,picto> palette::getpics(){ spritesheetmanager ssm("sprite_zone"); qlist<qstring> picnames = ssm.finder->allpic(); //get string displayed qhash<qstring,picto> piclist; for(int = 0; < picnames.size(); i++){ qstring picname = picnames.at(i); qgraphicstextitem *label = new qgraphicstextitem(); label->setplaintext(picname); qgraphicsrectitem *rect = new qgraphicsrectitem(); rect->setrect(0,i*20,50,20); label->setparentitem(rect); label->setpos(0,0); this->additem(rect); qdebug()<< rect->pos(); //always homecoming (0,0) } homecoming piclist; }

i've tried code several variation, can't solve this.

try this:

for(int = 0; < 5; i++){ qstring picname = "sample"; qgraphicstextitem *label = new qgraphicstextitem(); label->setplaintext(picname); qgraphicsrectitem *rect = new qgraphicsrectitem(); rect->setrect(0,i*50,50,20); label->setparentitem(rect); label->moveby(0,i*50);//new //or label->setpos(0,i*50); this->additem(rect); qdebug()<< rect->pos(); }

output of qdebug()<< rect->rect()<< label->scenepos();

qrectf(0,0 50x20) qpointf(0, 0) qrectf(0,50 50x20) qpointf(0, 50) qrectf(0,100 50x20) qpointf(0, 100) qrectf(0,150 50x20) qpointf(0, 150) qrectf(0,200 50x20) qpointf(0, 200)

result:

edit:

calling setrect() on qgraphicsrectitem not alter pos(), changes position of rectangle draws, item's position unchanged. can set rect (0, 0) , phone call setpos() move item want. next code different result on scene absolutely same.

for(int = 0; < 5; i++){ qstring picname = "sample"; qgraphicsrectitem *rect = new qgraphicsrectitem(); qgraphicstextitem *label = new qgraphicstextitem(); label->setplaintext(picname); rect->setrect(0,0,50,20); rect->setpos(0,i*50); //rect->moveby(0,i*50); label->setparentitem(rect); //label->setpos(0,i*50); this->additem(rect); }

in case pos() returns right value

c++ qt qgraphicsitem

No comments:

Post a Comment