Qt World Summit: Submit your Presentation

[solved]QML inside a QGraphicsView

  • Hello, I m using QGraphicsView / QGraphicsScene to design a game.
    I want to use QML to design the menu. How Can I add my QML code on the top of my GraphicsView ?

  • From docs:

    @QDeclarativeEngine engine;
    QDeclarativeComponent component(&engine);
    component.setData("import QtQuick 1.0\nText { text: "Hello world!" }", QUrl());
    QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(component.create());

    //add item to view, etc@

    Since QDeclarativeItem is QGraphicsObject you can add to your QGraphicsScene.

  • thanks! It's cute like Qt!:)

  • Warning! After some try, I get some crash using QFile and setData.
    So do not do :
    QFile file("main.qml"); {..}
    component.setData(file.readAll(), QUrl());@

    And prefer :

    @ mComponent = new QDeclarativeComponent(mEngine, QUrl::fromLocalFile("main.qml"));

Log in to reply