Maybe it’s just too simple

Published Saturday August 26th, 2006
Comments Off on Maybe it’s just too simple
Posted in Graphics Items, Graphics View, KDE, Labs, Qt

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 (http://lists.trolltech.com/) 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;
    scene.addPixmap(QPixmap("konqi.png"));

    QGraphicsView view(&scene);
    view.show();

    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"));
pixmap->setFlag(QGraphicsItem::ItemIsMovable);

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.");
text->setTextInteractionFlags(Qt::TextEditorInteraction);

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
{
public:
    Pixmap(const QPixmap &pixmap) : QGraphicsPixmapItem(pixmap) {
        setAcceptDrops(true);
    }

protected:
    void dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
        pix.load(QUrl(event->mimeData()->text()).path());
        event->setAccepted(!pix.isNull());
    }

    void dropEvent(QGraphicsSceneDragDropEvent *event) {
        setPixmap(pix);
    }

private:
    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

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in Graphics Items, Graphics View, KDE, Labs, Qt

Get started today with Qt Download now