0

I need to design and develop a kind of graphics piece of software, which can edit customized files containing graphical elements, etc.

I expect the piece of software to contain many documents thanks to the QMdiArea which is actually my central widget inside of my QMainWindow.

For each document, I will need both a QGraphicsView and a QGraphicsScene as well, since they work together.

Now, my question is, should I inherit QGraphicsView with a protected/private member to its own QGraphicsScene, or should I create a class which inherits QWidget and handles instances of QGraphicsView / QGraphicsScene ?

Or is there any solution left that I didn't think about?

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
Geoffrey R.
  • 1,907
  • 1
  • 19
  • 32

1 Answers1

1

First off, I don't think you need a QWidget to manage the QGraphicsScene and QGraphicsView. With that in mind, the "best practice" is typically to avoid subclassing if possible. Eventually you might have to subclass QGraphicsView (if you want to change its default functionality), but nothing in your question implies that you need to right now. Also note that there is a function QGraphicsView::scene() that returns the view's current scene, so there is no need to make the scene a member (it already is).

If you ever need to access a particular view or scene, you can do something like this:

MainWindow::onActionClearActiveWindow() // just an example
{
    QMdiArea *myMdiArea = static_cast<QMdiArea*>(centralWidget());
    QGraphicsView *activeView = static_cast<QGraphicsView*>(myMdiArea->widget());
    QGraphicsScene *activeScene = activeView->scene();
    activeScene->clear();
}

See also QMdiArea::subWindowList() which returns a list of all the sub windows.

Anthony
  • 8,570
  • 3
  • 38
  • 46
  • But the `scene` member of the `QGraphicsView` is set to null, isn't it? That is why I was thinking about subclassing the whole stuff and therefore create customised scene objects and the like. Anyway thanks for your help! – Geoffrey R. Oct 28 '12 at 14:03
  • 1
    The `scene` member is null when you first construct a QGraphicsView, but you could just do `view->setScene(new QGraphicsScene)`. That will create a scene for the view, and afterwards you can get a pointer to that scene by calling `view->scene()`. – Anthony Oct 28 '12 at 14:29
  • Anthony, I have another question: I will subclass `QGraphicsScene` so I will give it some custom behaviour, moreover I have to handle a collection of items. Shall I keep using `QGraphicsScene` / `QGraphicsView` or is it better to handle a class that encapsulates in attributes both of them? – Geoffrey R. Oct 30 '12 at 11:26
  • 1
    Yes definitely keeping QGraphicsScene and QGraphicsView! That's exactly what they are for. I think it is better to use them and keep them separate, rather than make a class to handle both the scene and view behavior. I think subclassing QGraphicsScene is quite normal, by the way. – Anthony Oct 30 '12 at 12:44
  • So, you think that I should subclass `QGraphicsScene` and therefore add attributes into it - e.g. wether the document has been saved or not - don't you? I am pleased by the idea but I am definitely looking for the best solution. Lastly I've subclassed `QGraphicsView` so I draw a surrounding rectangle without adding in to the `QGraphicsScene`, and that is why I wanted to do. What do you think about these practices? – Geoffrey R. Oct 30 '12 at 13:24
  • I think we're getting a little off topic here but I'll send you an email shortly. – Anthony Oct 30 '12 at 14:44