Results 1 to 5 of 5

Thread: QGraphicsScene size

  1. #1
    Join Date
    Jun 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QGraphicsScene size

    Hello.
    I'm trying to create simple drawing program.
    I'd like to create view of a sheet of paper on which user could draw and which could be zoomed. The similar effect like in MS Word page view.
    I've tried to add QGraphicsView as a main window widget and than add QGraphicsScene with specified Rect. But unfortunately QGraphicsScene always is expanded to the QGraphicsView size. If I draw sth bigger than QGraphicsScene specified area size it always is drawn whole - I want to draw only those part of item which contains in scene area.

    Is it possible to get such effect?

    PS. QGraphicsView have to be expanded to whole window area because when user is zooming, scene area should growth. So QGraphicsScene should have different size than QGraphicsView.

  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: QGraphicsScene size

    But unfortunately QGraphicsScene always is expanded to the QGraphicsView size.
    You mean that if you resize your view the "white" background "stretches" with it I guess.
    But it doesn't mean the scene is the size of the view.

    The scene is a logical entity, it has no graphical representation of its own.
    I urge you to read the QGraphicScene and QGraphicsView docs thoroughly, I am sure it will answer most of your problems.
    Ask here again if not.
    Last edited by high_flyer; 15th May 2011 at 16:21.
    ==========================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
    Jun 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    Thank you for your interest!

    I've ridden docs and I know about that. Let's say I have QGraphicsScene with 200x200px size. Window size (and QGraphicsView size) is about 1000x1000. Then I draw QGraphicsTextItem with string with at least 1000 chars. and draw it at (0,0) point. Unfortunatelly it is always! drawn on whole window (1000x1000) and it is not cropped to 200x200px. Moreover if I enable dragging of this text item I am able to move them on whole window - not only on 200x200px scene.

    Let me show you my code. Maybe I'm doing sth wrong and I can't see it.

    Methods of my QGraphicsView class:
    Qt Code:
    1. Scene::Scene(QWidget *parent) :
    2. QGraphicsView(parent),
    3. m_canvas(0)
    4. {
    5. setupGUI();
    6. }
    7.  
    8. Scene::Scene(Canvas * canvas, QWidget *parent) :
    9. QGraphicsView(canvas, parent),
    10. m_canvas(canvas)
    11. {
    12. setupGUI();
    13. }
    14.  
    15. Scene::~Scene()
    16. {
    17. closeCanvas();
    18. }
    19.  
    20. void Scene::setupGUI()
    21. {
    22. this->setAutoFillBackground(false);
    23. this->viewport()->setAutoFillBackground(false);
    24. }
    25.  
    26. void Scene::newCanvas(const QRectF & dimension)
    27. {
    28. closeCanvas();
    29. m_canvas = new Canvas(dimension, this);
    30. this->setScene(m_canvas);
    31. }
    32.  
    33. void Scene::closeCanvas()
    34. {
    35. if (m_canvas)
    36. {
    37. delete m_canvas;
    38. m_canvas = 0;
    39. }
    40. }
    41.  
    42. void Scene::wheelEvent(QWheelEvent* event)
    43. {
    44. double scaleFactor = 1.05;
    45. if (event->modifiers() & Qt::ControlModifier)
    46. scaleFactor += 0.3;
    47. if(event->delta() < 0)
    48. scaleFactor = 1.0 / scaleFactor;
    49. scale(scaleFactor, scaleFactor);
    50. centerOn( mapToScene(event->pos()) );
    51. }
    To copy to clipboard, switch view to plain text mode 

    Metho9ds of QGraphicsScene class:
    Qt Code:
    1. Canvas::Canvas(const QRectF & dimension, QObject * parent) :
    2. QGraphicsScene(dimension, parent)
    3. {
    4. QGraphicsRectItem * background = new QGraphicsRectItem(dimension,0,this);
    5. QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect();
    6. effect->setBlurRadius(20);
    7. effect->setOffset(5);
    8. background->setGraphicsEffect(effect);
    9. background->setBrush(Qt::white);
    10. QGraphicsTextItem * text = new QGraphicsTextItem("hudskjgifv j",0,this);
    11. text->setFlag(QGraphicsItem::ItemIsMovable, true);
    12. }
    13.  
    14. void Canvas::drawBackground(QPainter * painter, const QRectF & rect)
    15. {
    16. QGraphicsScene::drawBackground(painter, rect.intersect(this->sceneRect()));
    17. }
    18.  
    19. void Canvas::drawForeground(QPainter * painter, const QRectF & rect)
    20. {
    21. QGraphicsScene::drawForeground(painter, rect.intersect(this->sceneRect()));
    22. }
    To copy to clipboard, switch view to plain text mode 

    And at last here is the code where I create my window:
    Qt Code:
    1. KXmlGuiWindow * window = new KXmlGuiWindow();
    2. Scene * m_scene = new Scene(window);
    3. m_scene->newCanvas(QRectF(0, 0, 200.0, 200.0));
    4. window->setCentralWidget(m_scene);
    5. window->show();
    To copy to clipboard, switch view to plain text mode 
    Last edited by coder89; 13th May 2011 at 15:01.

  4. #4
    Join Date
    Jun 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene size

    Could anyone help me to solve this problem?
    Thank you in advance

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QGraphicsScene size

    If you are still looking for a solution, It may not be it is not possible to do directly, there is an alternate way (and I guess, QGraphicsScene is designed to be used this way)

    Size of the QGraphicsScene is by default unlimited, so set the required size, using QGraphicsScene::setSceneRect(), and QGraphicsView, will ensure to show all the size (it can show more than the set size, if the containing widget is larger). Setting the scene rectangle will not restrict the items being drawn outside the scene rectangle, this is because item draw them self and scene does not draw them, so items have to get the scene rectangle first and them draw themselves with in the scene rectangle.

    In your case, as you posted of using large text which may be drawn beyond the scene rectangle, you should first get the scene rectangle, and set restriction to draw wrapped test with in this rectangle.

Similar Threads

  1. how to check QGrapicsView/QGraphicsScene size
    By estel in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 10th February 2010, 00:38
  2. Replies: 12
    Last Post: 30th May 2009, 15:29
  3. Replies: 2
    Last Post: 23rd March 2009, 18:26
  4. QGraphicsScene size
    By invictus in forum Qt Programming
    Replies: 11
    Last Post: 12th August 2007, 22:26
  5. How can I make size of QGraphicsScene smaller?
    By troorl_ua in forum Qt Programming
    Replies: 6
    Last Post: 21st April 2007, 08:56

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.