Results 1 to 13 of 13

Thread: Problem with QGraphicsView/Scene coordinates

  1. #1
    Join Date
    Oct 2009
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with QGraphicsView/Scene coordinates

    Hello all!
    I have problem with changing position for QGraphicsTextItem's on QGraphicsView/Scene. I want pos them at upper left corner, here is the source of my method :


    Qt Code:
    1. void Model::setUpGraphicScene(QStandardItemModel *model,int size)
    2. {
    3. static QFont font("Times",9);
    4. int x = - this->vrptr->geometry().width(); //vrptr - pointer to QGraphicsView
    5. int y= - (this->vrptr->geometry().height()/2);
    6. QGraphicsItem * item;
    7. static QFontMetrics fn (font);
    8. static int yGrow = fn.height() - 3;
    9. this->visualResultModel->clear();//visualResultModel is a QGraphicsScene
    10. item= visualResultModel->addText(model->horizontalHeaderItem(0)->text(),font);
    11. item->setPos(x,y);
    12. for(int i=1;i<size;++i)
    13. {
    14. item= visualResultModel->addText(model->horizontalHeaderItem(i)->text(),font);
    15. item->setPos(x,y+yGrow);
    16. y=item->pos().y();
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    here is result :

    i.e. there is scrolling appears, why?
    Thanks.
    And sorry for my bad english

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsView/Scene coordinates

    Well, the first thing that comes to mind is that one of the strings is very long, probably filled with spaces or tabs - can you check and make sure this is not the case?

    Oh and why do you asign your x so:
    Qt Code:
    1. int x = - this->vrptr->geometry().width();
    To copy to clipboard, switch view to plain text mode 

    and not just x= 0; ?

    it might be that you are placing your string out side the initially visible scene, hence the scroll bar appears.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2009
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsView/Scene coordinates

    Quote Originally Posted by high_flyer View Post
    Well, the first thing that comes to mind is that one of the strings is very long, probably filled with spaces or tabs - can you check and make sure this is not the case?

    Oh and why do you asign your x so:
    Qt Code:
    1. int x = - this->vrptr->geometry().width();
    To copy to clipboard, switch view to plain text mode 

    and not just x= 0; ?

    it might be that you are placing your string out side the initially visible scene, hence the scroll bar appears.
    No,all strings does not have spaces or tab, and case etc..
    x...hm i think, if i assign x=0 it will be at center of view,no?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsView/Scene coordinates

    x...hm i think, if i assign x=0 it will be at center of view,no?
    It depends on how you set your coordinate system.
    But even if they apear in the middle, it is still in the visible scene area, and if my guess is correct, you will not get the scroll bar.
    Make sure you place the string in side the visible scene area, then the scroll bar will not appear.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Oct 2009
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsView/Scene coordinates

    Quote Originally Posted by high_flyer View Post
    It depends on how you set your coordinate system.
    But even if they apear in the middle, it is still in the visible scene area, and if my guess is correct, you will not get the scroll bar.
    Make sure you place the string in side the visible scene area, then the scroll bar will not appear.
    I'm sorry, you did not understand me a little, i want pos my items' at upper left corner without scrollbar
    And how to set the coordinate system?
    I wish that would be the upper left corner was 0.0

  6. #6
    Join Date
    Oct 2009
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsView/Scene coordinates

    So, anyone can help me?

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsView/Scene coordinates

    Regarding your x position, why are you not setting it to half of the width like you did with the height?:
    Qt Code:
    1. int x = - this->vrptr->geometry().width()/2;
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Oct 2009
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsView/Scene coordinates

    Quote Originally Posted by high_flyer View Post
    Regarding your x position, why are you not setting it to half of the width like you did with the height?:
    Qt Code:
    1. int x = - this->vrptr->geometry().width()/2;
    To copy to clipboard, switch view to plain text mode 
    Becouse, when i set it to -half... :


  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsView/Scene coordinates

    ok, so now as you see, the scroll bar is not appearing any more (your original problem).
    So the only thing you have to do now is to figure out the correct position coordinates, such, that you don't position your strings outside the visible scene area and all is well.

    EDIT:
    it looks like the scene is larger then your QGraphicsView, which makes the center of the scene more to the right of the viewing area.
    Resize the scene to the size of the viewing area, and then
    Qt Code:
    1. int x = - this->vrptr->geometry().width()/2;
    To copy to clipboard, switch view to plain text mode 

    should work.
    Last edited by high_flyer; 16th October 2009 at 16:32.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. The following user says thank you to high_flyer for this useful post:

    Andrewshkovskii (19th October 2009)

  11. #10
    Join Date
    Oct 2009
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsView/Scene coordinates

    Quote Originally Posted by high_flyer View Post
    ok, so now as you see, the scroll bar is not appearing any more (your original problem).
    So the only thing you have to do now is to figure out the correct position coordinates, such, that you don't position your strings outside the visible scene area and all is well.
    The incorrect coordinates/position was my problem first
    either because of no knowledge of English, either because of stupidity, I can not understand your 2nd sentence) You mean,what i pos my string outside the the visible scene first time?But what i need to do, to solve the problem with position strings at upper lefr corner?M.b i need to set QGraphicsView::setSceneRect to View's rect? or something else?

  12. #11
    Join Date
    Oct 2009
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsView/Scene coordinates

    oh...so setSceneRect, as i think,will solve my problem ,yeath?

  13. #12
    Join Date
    Oct 2009
    Posts
    27
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsView/Scene coordinates

    if i do like this :

    Qt Code:
    1. void Model::setUpGraphicScene(QStandardItemModel *model,int size)
    2. {
    3. this->visualResultModel->setSceneRect(this->vrptr->rect());
    4. static QFont font("Times",9);
    5. int x = - (this->vrptr->geometry().height()/2);
    6. int y= - (this->vrptr->geometry().height()/2);
    7. QGraphicsItem * item;
    8. static QFontMetrics fn (font);
    9. static int yGrow = fn.height() -3;
    10. this->visualResultModel->clear();
    11. item= visualResultModel->addText(model->horizontalHeaderItem(0)->text(),font);
    12. item->setPos(x,y);
    13. for(int i=1;i<size;++i)
    14. {
    15. item= visualResultModel->addText(model->horizontalHeaderItem(i)->text(),font);
    16. item->setPos(x,y+yGrow);
    17. y=item->pos().y();
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    That's what happens


    i.e items pos() out of viewport area

  14. #13
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry Re: Problem with QGraphicsView/Scene coordinates

    Quote Originally Posted by Andrewshkovskii View Post
    if i do like this :

    Qt Code:
    1. void Model::setUpGraphicScene(QStandardItemModel *model,int size)
    2. {
    3. this->visualResultModel->setSceneRect(this->vrptr->rect());
    4. static QFont font("Times",9);
    5. int x = - (this->vrptr->geometry().height()/2);
    6. //Are your sure it is correct to set a x coordinate with height()?
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 
    I think you need to re-thinking your item management, since you seems to not use graphics features as usual way.

    QGraphicsView is a view of a QGraphicsScene.
    When unsing QGraphicsItem::setPos, you are positionning item in the scene as data regarless of the view portion of the scene displayed in the view.
    So the point(0,0) is the center of the scene, whatever shows the view.

    To put your item at topleft corner of the view, you can use QGraphicsView::mapToScene to convert pixel coordinates of the viewport to scene coordinates. see the-graphics-view-coordinate-system

    But if you dont move the view, every item will be at same position!

    But this still not explain why your sceneRect grows in y-axis...

Similar Threads

  1. Replies: 19
    Last Post: 3rd April 2009, 23:17
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.