Results 1 to 11 of 11

Thread: QList QgraphicsItem pos x value returns 0

  1. #1
    Join Date
    Sep 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default QList QgraphicsItem pos x value returns 0

    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.


    Qt Code:
    1. QList<QGraphicsItem*> Note::getNotesInMeasure()
    2. {
    3. extern QGraphicsScene* scene;
    4. QList<QGraphicsItem*>iList;
    5.  
    6. iList = scene->selectedItems();
    7.  
    8. if (!( iList.isEmpty() || iList.size() == 0 )){
    9. int sz = iList.size();
    10. int i;
    11. for (i=0; i<sz; i++){
    12. item = iList.value(i);
    13. int xxx = item->pos().x();
    14. QString n = item->data(2).toString();
    15. QString h = item->data(0).toString();
    16. if ( n == "Note" ) { msg(h); }
    17. }
    18.  
    19. }
    20. return iList;
    21. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList QgraphicsItem pos x value returns 0

    Why do you claim item->pos().x() returning 0 is an incorrect result? What does it have to do with item->data()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList QgraphicsItem pos x value returns 0

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList QgraphicsItem pos x value returns 0

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList QgraphicsItem pos x value returns 0

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList QgraphicsItem pos x value returns 0

    So how do you create those items? What rectangles do you pass for the ellipses?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList QgraphicsItem pos x value returns 0

    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.
    Last edited by devdon; 19th September 2011 at 22:57.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList QgraphicsItem pos x value returns 0

    Do you then call setPos() on your items?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList QgraphicsItem pos x value returns 0

    I do not call setpos on the ellipses.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList QgraphicsItem pos x value returns 0

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList QgraphicsItem pos x value returns 0

    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.
    Last edited by devdon; 20th September 2011 at 09:48.

Similar Threads

  1. Qlist<QLabel *> in Qlist<QAction*>
    By Naahmi in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2011, 08:53
  2. Replies: 4
    Last Post: 20th August 2010, 13:54
  3. Whats wrong with QList<QGraphicsItem>
    By rubenvb in forum Newbie
    Replies: 6
    Last Post: 22nd January 2010, 19:01
  4. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 18:27
  5. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.