PDA

View Full Version : QList QgraphicsItem pos x value returns 0



devdon
19th September 2011, 21:08
Hello,

This incomplete func compiles and works. But item->pos().x(); always returns 0 to the variable xxx when run in the debugger (Win 7, mingw release w/ sdk). Note that the following data variables are filled properly from the items so we know that the rest of the mechanism is working. Could this actually be a bug in QT 4.7 or have I misunderstood the value function? I'm stumped on this one. Thanks.




QList<QGraphicsItem*> Note::getNotesInMeasure()
{
extern QGraphicsScene* scene;
QList<QGraphicsItem*>iList;
QGraphicsItem* item;

iList = scene->selectedItems();

if (!( iList.isEmpty() || iList.size() == 0 )){
int sz = iList.size();
int i;
for (i=0; i<sz; i++){
item = iList.value(i);
int xxx = item->pos().x();
QString n = item->data(2).toString();
QString h = item->data(0).toString();
if ( n == "Note" ) { msg(h); }
}

}
return iList;
}

wysota
19th September 2011, 21:32
Why do you claim item->pos().x() returning 0 is an incorrect result? What does it have to do with item->data()?

devdon
19th September 2011, 21:39
Here's what happening on screen: there are musical notes selected by the rubberband. The routine takes the selectedItems, and item->pos().x() is supposed to return the x coordinate of each note. The other data routines provide additional info needed for other purposes. The data is coming through for the various notes, so the itemlist is being iterated properly but the x coordinate is not being returned from item->pos().x().Each one returns 0. Now, item->pos().x() works in other circumstances but not here for some reason. The docs say it should work. What am I missing?

wysota
19th September 2011, 21:57
How do you represent each note? How is the boundingRect() defined for the class representing the note? What is the parent object of each note?

devdon
19th September 2011, 21:59
Each note is a standard QGraphpicEllipseItem. I have not subclassed it at this point so the bounding rect should be pure standard issue. I have not assigned a specific parent to each note.

wysota
19th September 2011, 22:36
So how do you create those items? What rectangles do you pass for the ellipses?

devdon
19th September 2011, 22:42
Note* myNote = new Note;
myNote->setRect(gridX-(w/2), (y-(h/2)), w, h); // w= width, h == height, gridX is a grid-altered x location, y is the location on the staff vertically.

Also, I stated something wrong earlier. I have a class called Note that is of QObject and QGraphicsEllipseItem. I create a new Note and set that rect using the above line. I hope this is more helpful.

wysota
19th September 2011, 22:56
Do you then call setPos() on your items?

devdon
19th September 2011, 23:03
I do not call setpos on the ellipses.

wysota
19th September 2011, 23:58
So position of all your items is (0,0) and the value returned in your code snippet is correct. I guess you are incorrectly assuming that pos of your item should be equal to the centre of the item's bounding rect (gridX, y). That is not the case. To get that, create your items with rects (-w/2, -h/2, w, h) and then call setPos(gridX, y).

devdon
20th September 2011, 08:46
Your fix worked. Thank you very much. It saved me great frustration.

Added after 56 minutes:

Now all the calculations for my children objects, such as the stems and flags, are way off to the right and down + x +y. But by taking the position of theNote, which I can now do thanks to you, I can position the child objects using your method and all is well. Thanks again.