Important: Please read the Qt Code of Conduct -

Reloading QDeclarativeView

  • I am developing a Qt application where i have a QDeclarativeView which I load a QML file into. I have also a QPlainTextEdit in another tab in the same application where the source of the QML lodde from the file is shown. If I change something in the QML source, I would like the QDeclarativeView to show the change.

    What I tried was to have a button which saved the change to the same file, and then call the setSource function to the declarativeView with the path to the file. But that doesn't work, the changes is not showing in the declarativeView. If I call close to the view, the same thing.

    What should I do to make the view showing the changes? The only thing that work is to quit the application, and load it upon relaunching it.

  • I dont know if this will help, but there are a cache mechanism that can cause problems when reading
    dynamic changing files (with same name):

    My QPushButton Code:
    void Dialog::on_pushButton_clicked()
    QFile f( "/home/yurii/test.qml" ); QIODevice::WriteOnly );
    if( f.isOpen() )
    ui->declarativeView->engine()->clearComponentCache(); // THIS IS WHAT YOU WANT
    f.write( ui->plainTextEdit->toPlainText().toAscii().data() );

        ui->declarativeView->setSource( QUrl::fromLocalFile("/home/yurii/test.qml") );


    QML Code inside a QPlainText:
    import Qt 4.7

    Rectangle {
    width: 100
    height: 100
    Text { text: "Hello world!" }

  • Thank you, clearComponentCache() worked perfect. By the way, is there a way to load a QML into QDeclarativeView directly from a QString, or is the only way to use setSource from a file?

  • As in documentation:

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


    QDeclarativeItem objects can be placed on a standard QGraphicsScene and displayed with QGraphicsView. QDeclarativeView is a QGraphicsView subclass provided as a convenience for displaying QML files, and connecting between QML and C++ Qt objects.

Log in to reply