Results 1 to 8 of 8

Thread: QGraphicsItem in QGraphicsScene - Painting items to x,y coordinates...

  1. #1
    Join Date
    Jul 2009
    Location
    Orange County California
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question QGraphicsItem in QGraphicsScene - Painting items to x,y coordinates...

    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:

    Qt Code:
    1. QGraphicsScene *m_pqMainScene;
    2. QGraphicsItem *m_paItem[MAX_NUMBER_OF_ITEMS];
    3.  
    4. int xPos = 0, yPos = 395;
    5. int itemHeight = 15;
    6. int horizontalLineLength = (numberOfItems() * itemHeight);
    7.  
    8. // Horizontal line
    9. for (long lIndex = 1; lIndex <=numberOfFromItems(); lIndex++)
    10. {
    11. m_paItem[lIndex] = new QGraphicsLineItem(0, 0, horizontalLineLength, 0, 0);
    12. m_paItem[lIndex]->setPos(xPos, yPos);
    13. m_pqMainScene->addItem(m_paItem[lIndex]);
    14. yPos -= itemHeight;
    15. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong???

    Thanks!

  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: QGraphicsItem in QGraphicsScene - Painting items to x,y coordinates...

    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.
    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
    Jul 2009
    Location
    Orange County California
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem in QGraphicsScene - Painting items to x,y coordinates...

    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.

  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: QGraphicsItem in QGraphicsScene - Painting items to x,y coordinates...

    So what is the difference between what you obtain and what you would like to obtain?
    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
    Jul 2009
    Location
    Orange County California
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Red face Re: QGraphicsItem in QGraphicsScene - Painting items to x,y coordinates...

    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.

  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: QGraphicsItem in QGraphicsScene - Painting items to x,y coordinates...

    Shouldn't it be something like:

    Qt Code:
    1. yPos = sceneRect().bottom(); // instead of 395
    To copy to clipboard, switch view to plain text mode 
    ?
    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. The following user says thank you to wysota for this useful post:

    ikeurb (16th July 2009)

  8. #7
    Join Date
    Jul 2009
    Location
    Orange County California
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem in QGraphicsScene - Painting items to x,y coordinates...

    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.

    Qt Code:
    1. // Horizontal line
    2. for (long lIndex = 1; lIndex <=numberOfFromItems(); lIndex++)
    3. {
    4. m_paItem[lIndex ] = new AGridLine(xPos, yPos, horizontalLineLength, yPos);
    5. m_pScene->addItem(m_paItem[lIndex]);
    6. yPos -= padding;
    7. }
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem in QGraphicsScene - Painting items to x,y coordinates...

    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?

    Qt Code:
    1. m_graphicsView.scale(1, -1);
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  2. Replies: 1
    Last Post: 31st December 2008, 16:55
  3. Replies: 0
    Last Post: 24th November 2008, 08:52
  4. Retrieving mouseover coordinates of QGraphicsScene
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2006, 18:36
  5. QGraphicsItem problem - how to save items info ??
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 17th October 2006, 12:17

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.