Maybe it’s just too simple

I've did some web crawling on Graphics View [keywords graphicsview, qgraphicsview, qgraphicsscene, "graphics view"...], trying to get a feel for what projects out there are poking into Qt 4.2's snapshots, and starting to use our favorite canvas framework for real-life apps. I was positively surprised. Try it out :-). People are discussing the frame work on numerous newsgroups and mailing lists, and projects in the community are popping up one after another. That's pretty good for a framework that's still not released.

Also reading all the excellent feedback the framework has gotten on the tech-preview mailing lists ( gives me a good feeling for 4.2, and for the future of Qt 4 as a tool for writing some awesome graphical apps.

Then I read something about how some folks find the Graphics View framework too hard to use, being forced onto the model-view mindset and all. Is it really that hard? Consider the following piece of code:

int main(int argc, char **argv) {
QApplication app(argv, argv);

QGraphicsScene scene;

QGraphicsView view(&scene);;

return app.exec();

I'm not sure if it's the extra line that declares the scene that accounts for complexity, but the above code looks pretty simple to me, easy to read and understand too. If you want to be able to move a pixmap item around with the mouse, you can just enable a flag:

QGraphicsItem *pixmap = scene.addPixmap(QPixmap("konqi.png"));

Try it out :-). It's quite straight-forward. That pixmap item could be a knight chip (chess?), an integrated chip, you name it. And look how easy it is to create a full-blown text editor item:

QGraphicsTextItem *text = scene.addText("A paragraph of text.");

Click on it and start typing, selecting, and so on. Even drag&drop and context menus work out of the box.

Even complex features are easy with Graphics View. Take drag&drop. With Graphics View it's almost too simple. ;-) Here's how to implement a pixmap item that receives its contents via drag&drop. You can try it by dragging images from you favorite file manager:

class Pixmap : public QGraphicsPixmapItem
Pixmap(const QPixmap &pixmap) : QGraphicsPixmapItem(pixmap) {

void dragEnterEvent(QGraphicsSceneDragDropEvent *event) {

void dropEvent(QGraphicsSceneDragDropEvent *event) {

QPixmap pix;

Enable drag&drop with setAcceptDrops(). Accept the drag enter if the proposed pixmap is valid. Set the pixmap in dropEvent(). How much easier can it get?

Now let's use this new item:

scene.addItem(new Pixmap("konqi.png"));

I personally cannot understand why someone would want to reinvent this wonderful framework, now that it's available.

Ah well, now I've said it.

/me sips some more coffee

Blog Topics: