
Originally Posted by
Shuchi Agrawal
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
MainWindow::MainWindow()
{
// Here scene is supposed to be member variable since u need it later
// also you can give any geometry of scene
view->setScene(scene);
...
}
MainWindow::MainWindow()
{
QGraphicsView *view = new QGraphicsView(this);
// Here scene is supposed to be member variable since u need it later
// also you can give any geometry of scene
scene = new QGraphicsScene(0,0,1024,800);
view->setScene(scene);
...
}
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
MainWindow::slotOnMenuClick()
{
scene->addItem(line);
}
MainWindow::slotOnMenuClick()
{
QGraphicsLineItem *line = new QGraphicsLineItem( required line);
scene->addItem(line);
}
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.
Bookmarks