Results 1 to 11 of 11

Thread: QGraphicsView, QGraphicsItem, QGraphicsScene

  1. #1
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Question QGraphicsView, QGraphicsItem, QGraphicsScene

    hi,
    can anyone make me clear with the difference b/w QGraphicsView, QGraphicsItem and QGraphicsScene . i studied fm the documentation but still i m not clear how to use them.
    i want to draw a line on a menu click or when i click at a point n drag n release. so where to write code for this? in QGraphicsView, QGraphicsItem or QGraphicsScene ?
    thanks in advance
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView, QGraphicsItem, QGraphicsScene

    Quote Originally Posted by Shuchi Agrawal View Post
    hi,
    can anyone make me clear with the difference b/w QGraphicsView, QGraphicsItem and QGraphicsScene . i studied fm the documentation but still i m not clear how to use them.
    i want to draw a line on a menu click or when i click at a point n drag n release. so where to write code for this? in QGraphicsView, QGraphicsItem or QGraphicsScene ?
    thanks in advance
    GraphicsView framework is pretty well organised and documentation is also good. You only need to read, do the examples and tutorials.
    Anyway to put it in simple words QGraphicsScene is the offscreen container for QGraphicsItem's. QGraphicsItem are objects that represent the shapes or anything to be drawn and managed. QGraphicsView is the viewer widget which enables to "view" the scene.

    For your second question
    Just create QGraphicsView and QGraphicsScene in your mainwindow constructor like this

    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. QGraphicsView *view = new QGraphicsView(this);
    4. // Here scene is supposed to be member variable since u need it later
    5. // also you can give any geometry of scene
    6. scene = new QGraphicsScene(0,0,1024,800);
    7. view->setScene(scene);
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    Now just create QGraphicsLineItem's when you need to draw line. For eg assuming the below slot to be called from menu do the following
    Qt Code:
    1. MainWindow::slotOnMenuClick()
    2. {
    3. QGraphicsLineItem *line = new QGraphicsLineItem( required line);
    4. scene->addItem(line);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Drawing lines on clicking and dragging needs you to reimplement QGraphicsScene and in that reimplement mouse*Events() which will require some knowledge about the framework. Go through the examples and documentation.
    Last edited by Gopala Krishna; 30th January 2007 at 12:06. Reason: spelling error
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. The following user says thank you to Gopala Krishna for this useful post:

    Shuchi Agrawal (31st January 2007)

  4. #3
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView, QGraphicsItem, QGraphicsScene

    hey thanks a lot but i m able to draw line son clicking n all but i m unable to map the coordinates and this is where i need help. as u said tht in QGraphicsScene i need to write the code.. so i wil implement n let u know how much i succeeded..
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  5. #4
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView, QGraphicsItem, QGraphicsScene

    hi,
    can anyone help m enderstanding the concept of coordinates for QGraphicsView, QGraphicsItem, QGraphicsScene (i hv read the Qt doc bt still not able to nderstand)..

    graphicsView = new QGraphicsView(centralwidget);
    graphicsView->setGeometry(QRect(0, 0, 250, 250));

    scene = new QGraphicsScene();
    scene->setSceneRect(0,0,250,250);
    view->setScene(scene);

    QGraphicsLineItem *line = new QGraphicsLineItem(10.0,10.0,50.0,50.0);
    line->setFlag( QGraphicsItem::ItemIsMovable );
    scene->addItem(line);

    what is the relationship of the 3 coordinates given in 3 diff lines of code?
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  6. #5
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView, QGraphicsItem, QGraphicsScene

    Quote Originally Posted by Shuchi Agrawal View Post
    (i hv read the Qt doc bt still not able to nderstand)
    Qt docs aren't that bad. I hope you have read this . Go through them slowly and just experiment by writing small programs. For eg you can write a small program and reimplement QGraphicsItem, QGraphicsScene and then just output the coordinates wherever you feel. You can alo try rotating items on right click for eg to get feel of coordinate system.

    Quote Originally Posted by Shuchi Agrawal View Post
    hi,
    can anyone help m enderstanding the concept of coordinates for QGraphicsView, QGraphicsItem, QGraphicsScene .
    If you didn't understand from reading this .

    Each entity (Item,View and Scene) have their own coordinate sytem for convienience.
    The coordinate system of QGraphicsItem is for the item to mange itself locally. boundingRect(), paint() , shape() .. all functions makes one really easy to implement them since you just need to use local(Item's) coordinates.
    For eg if you are implementing a card game and card being derived from QGraphicsItem.
    Lets assume size of card to 100 pixels by 80 pixels.
    Then
    Qt Code:
    1. QRectF Card::boundingRect()
    2. {
    3. // The following is just according to local coordinates centred at (0,0)
    4. return QRectF( -50,-40,100,80);
    5. }
    6. void Card::paint(QPainter *p,..)
    7. {
    8. p->drawRect(QRectF(-50,-40,100,80));
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 
    Now the coordinates specified in above are item's coordinates. You need not bother about where exactly is the item present on scene and hence not to bother with scene at all while implemeting any item. This is huge advantage since you can easily do all kinds of transformations (rotations,shearing,scaling..) on single item. So you can infact have any item ,for example a line, be rotated on right click and to do this you just need to write maximum of three lines of code in mousePressEvent() which would otherwise made you to do all calculations to required coordinates all by yourself and then maintain the item in that angle , you can imagine more!!!!

    On the other hand scene coordinate system is used to manage all items globally. For eg you can rotate all items in scene by just setting appropriate QMatrix. Updating scene will update all the views (any no of views can have same scene) and hence if you do transformations to scene, all views will have transformations.

    Doing transformation on view will just transform everything only in that view.In short you can easily transform individual items, just particular view or the scene(all views).


    Hope now you get some idea about coordinate system in The GraphicsView Framework.

    Quote Originally Posted by Shuchi Agrawal View Post
    graphicsView = new QGraphicsView(centralwidget);
    graphicsView->setGeometry(QRect(0, 0, 250, 250));

    scene = new QGraphicsScene();
    scene->setSceneRect(0,0,250,250);
    view->setScene(scene);

    QGraphicsLineItem *line = new QGraphicsLineItem(10.0,10.0,50.0,50.0);
    line->setFlag( QGraphicsItem::ItemIsMovable );
    scene->addItem(line);

    what is the relationship of the 3 coordinates given in 3 diff lines of code?
    View's geometry is just geometry of Widget.
    Scene rect is geometry of scene - size of canvas.
    The coordinates you are giving in constructor of line is just the start and end pos of line in 'scene' coordinates. Here don't get confused between item and scene coordinates. To specify position of item you should give the location of item on scene. Remember item coordinates are used while overriding QGraphicsItem while the location of item on scene is provided by scene coordinates.
    Last edited by Gopala Krishna; 31st January 2007 at 17:20. Reason: missing [code] tags
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  7. #6
    Join Date
    Dec 2006
    Posts
    103
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QGraphicsView, QGraphicsItem, QGraphicsScene

    hi Gopala,
    thanks a lot for ur valuable help. i m able to make out the difference.now i want to zoom an item. i m able to do that but in zooming the item i want tht whatever part of that item i click, should be zoomed(obviously with the item itself) and displayed..
    i m attaching some screen shots.
    problem 1:
    the initial execution is shot1.now when i m right clicking on the circle then its zooming as in shot2. but if i want to click on east south corner of the circle then tht part shd be seen on the view not the west north part as i have shown in shot3.. plz see to it..

    Qt Code:
    1. void Item::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. if (event->buttons() & Qt::RightButton)
    4. {
    5. delta1+=0.2;
    6. scale(delta1,delta1);
    7. ensureVisible((event->pos()).x(),(event->pos()).y(),50,50,50,50);
    8. //setPos((event->pos()).x(),(event->pos()).y());
    9. }
    10. else
    11. QGraphicsItem::mousePressEvent(event);
    12. //p1 = event->buttonDownScenePos(Qt::LeftButton);
    13. }
    14.  
    15. QRectF Item::boundingRect() const
    16. { return QRectF(0,0,60,60); }
    17.  
    18. QPainterPath Item::shape() const
    19. { QPainterPath path;
    20. path.addEllipse(5, 5, 50, 50);
    21. return path; }
    To copy to clipboard, switch view to plain text mode 

    problem 2:
    when i m clicking "draw" then i m getting shot4. but when i m moving different items like square,circle,line,coloured ellipse or QGraphicsTextItem("shuchi") then many times the other item which is not clicked also get moved. why????

    Qt Code:
    1. void MainWindow::draw()
    2. {
    3. QGraphicsLineItem *line = new QGraphicsLineItem(10.0,10.0,50.0,50.0);
    4. line->setFlag( QGraphicsItem::ItemIsMovable );
    5. scene->addItem(line);
    6.  
    7. QGraphicsRectItem *rect = new QGraphicsRectItem(0, scene);
    8. rect->setRect(60,60,90,90);
    9. rect->setFlag( QGraphicsItem::ItemIsMovable );
    10.  
    11. QGraphicsEllipseItem *ellipse = new QGraphicsEllipseItem( 0, scene );
    12. ellipse->setRect( 50, 50, 50, 50 );
    13. ellipse->setFlag( QGraphicsItem::ItemIsMovable );
    14.  
    15. text1 = new MyTextItem("Shuchi", 0, scene);
    16. text1->setPos(150,150); }
    To copy to clipboard, switch view to plain text mode 

    shots.zip
    Last edited by wysota; 23rd March 2011 at 19:08.
    I worked on windows Xp with Qt 4.2.2(Open Source Version) and MinGw
    now i am trying the same things on Fedora Core 5 (linux-gcc) and Qt 4.2.2 open source edition.

  8. #7
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView, QGraphicsItem, QGraphicsScene

    Hello.

    First of all, sorry for reviving a VERY old thread, but I felt that expanding on a thread that already revolves around my issue would be better than increasing the size of the search results. If, for any reason, this is unapropiate behaviour, please do leet me know.

    Now to my problem.

    I'm trying, on a very simple graphic, to explain, as intuitively as possible, how QGraphicsScene - QGraphicsView and QGraphicsItem work together and relate themselves with each other. The reason for this is that I feel that, at times, QT's documentation can be too technical. While that is indeed the right thing to do, this might be annoying at times, so I'm writting my own "Guide to" QT to also teach myself in the process.

    Please, look at this picture:

    Untitled.jpg

    Is my understanding of them wrong? How would you improve on that?

    As always, if I'm coming short on data or on explaining myself, let me know.

    Thanks in advance for your help.

    A.

  9. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView, QGraphicsItem, QGraphicsScene

    I think you got it right.
    Scene is just a container for managing graphics items, and rendering it with QGraphicsView is one of possible methods to visualize scene's content (you can render it with QPainter attached to any paint device).
    Maybe this is not very good example, but anyway - when you are at theater, actors are QGraphicsItems, scene is QGraphicsScene and each one of spectators is a separate QGraphicsView ( seeing the scene in slightly different way ).

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

    alitoh (23rd March 2011)

  11. #9
    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: QGraphicsView, QGraphicsItem, QGraphicsScene

    I always tell to my students that the scene is the world, the view is the window they're looking through at the world and the items are things that are part of the world that one sees through the window. The world is self-sufficient (you don't need to have any windows for the world to be there and be useful) and objects in the world form hierarchies where children are positioned in a "local world" of their parent -- like people on Earth, Earth in the Solar System, Solar System in the Milky Way, Milky Way in the local cluster of galaxies, etc. And people don't care that Milky Way rotates around the centre of the local cluster, they use local coordinates relative to the Earth exactly the same way as items in the scene don't care about surroundings of its parent and use local coordinates relative to their parent.
    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.


  12. The following 2 users say thank you to wysota for this useful post:

    alitoh (23rd March 2011), cgyan1 (11th March 2012)

  13. #10
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView, QGraphicsItem, QGraphicsScene

    Thank you for your answers!

    To extend on this issue a little bit more.

    Are the QGraphicsScene coordinates relative to the Dlg window? That is; the single pixel left-top corner below the dialog bar, 0,0, is also the Scene's 0,0?

  14. #11
    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: QGraphicsView, QGraphicsItem, QGraphicsScene

    Again, QGraphicsScene is the world. The widget (view) is a window in a house. Coordinates of the world are not relative to the window in the house because the house may not even exist. Of course it is possible to map coordinates of each place in the world relative to the glass in the window in the house provided the window in the house exists. This is called a projection and its properties change if you come closer or move away from the window surface. This doesn't change the world (scene) in any way although if you change your position, you see a different part of the world. So to answer your question the top-left corner of the window is not the top-left corner of the world.
    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.


  15. The following 2 users say thank you to wysota for this useful post:

    alitoh (24th March 2011), cgyan1 (11th March 2012)

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  2. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04
  3. QGraphicsScene and QThread
    By tts80 in forum Qt Programming
    Replies: 5
    Last Post: 10th January 2007, 09:32
  4. QGraphicsView and QGraphicsScene speeds
    By jnk5y in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2006, 07:13
  5. (QT4.2-RC1) QGraphicsScene QGraphicsView QGraphicsItem
    By antonio.r.tome in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 10:56

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.