[Solved] How to call refresh method(s) in QML/C++



  • Hi guys!

    I have a problem with refresh all components in my QML app. I have created a many QML files as small visual component for design. The application must get access to the files in the user's folder. So, I need to use open file for read informations from it and display it to user using QML components. I don't know how I can do it using C++ or QML. The source of main.qml file looks like this:

    @import QtQuick 1.1
    import Qt 4.7
    import Mdl 1.0

    Rectangle {
    id: mainWindow
    width: 1024
    height: 768
    color: "transparent"
    clip: true

    MdlLogViewerManager {
        property variant geoIp
        id: viewer
        objectName: "viewer"
    

    parser: MdlLogParser {
    // logFilePath: ":/log_map_MdlTest_1347293543.mdl"

            onParserDidFinished: {
                console.log("Refresh method(s) must be called here!!!");
            }
    

    }
    }

    Rectangle {
        anchors.fill: parent
        color: "transparent"
        clip: true
    
        LeftSideBar {
            id: leftBar
            color: "black"
            width: 400
            height: parent.height
            x: -leftBar.width
    
            NumberAnimation {
                running: true
                target: leftBar
                property: "x"
                duration: 700
                easing.type: Easing.InOutBack
                to: 0
            }
         }
    
        ContentView {
    

    id:contentView
    width: parent.width - 400
    height: parent.height
    x: leftBar.width
    y: -parent.width
    color: "transparent"

            NumberAnimation { running: true
                target: contentView
                property: "y"
                duration: 1000
                easing.type: Easing.InOutBack
                to: 0
            }
        }
    }
    

    }
    @

    The MdlLogViewerManager is a QObject subclass which work with file contents. When object is finished work his send a signal about it and main QML component received it. So, what I must added to my QML or C++ code for redraw all components? Thanks.

    P.S. The method for opening file looks like this:
    @ QString fileName = QFileDialog::getOpenFileName(this, tr("Open MDL Log"),
    "",
    tr("MDL Files (*.mdl)"));

    if(!fileName.isEmpty())
    {
        QDeclarativeItem *rootObject = dynamic_cast<QDeclarativeItem *>(viewer->rootObject());
        viewerManager = (MdlLogViewerManager*)rootObject->findChild<QObject *>(QString("viewer"));
        viewerManager->getParser()->setLogFilePath(fileName);
        //rootObject->update(rootObject->x(), rootObject->y(), rootObject->width(), rootObject->height());
        viewer->update();
        //viewer->setMainQmlFile&#40;QLatin1String("qml/MdlViewer/main.qml"&#41;&#41;;
    }
    

    @

    Please tell me what I do wrong. Thanks for the any help!



  • Hello,

    An update or redraw is not really a solution, i think, as you may later need to keep some value of your ui.

    If you want to call a C++ function, you can use a contexed object, ie use QDeclarativeContext to reach a C++ object from QML.

    Then, if your method is declared as Q_INVOKABLE (or is a slot) you can interact with it from QML easily.

    really an update seems a bad idea!



  • [quote author="dmcr" date="1352974106"]Hello,

    An update or redraw is not really a solution, i think, as you may later need to keep some value of your ui.

    If you want to call a C++ function, you can use a contexed object, ie use QDeclarativeContext to reach a C++ object from QML.

    Then, if your method is declared as Q_INVOKABLE (or is a slot) you can interact with it from QML easily.

    really an update seems a bad idea!
    [/quote]

    Thanks for the reply!

    I have a methods for get all informations from C++ objects but I don't know how I can change values for QML component which using it. For example, I have a component which show some parameter as table. When I load app it's clear and when user opened a some file this component must show a new data. In C++ object all data was updated correctly. So, the question is how I can change value in QML? Must I added code like this:

    @ QDeclarativeItem item = qobject_cast<QDeclarativeItem>(object);
    item->setWidth(500);@

    for update every data or exist more powerfully way? I need update a may component, and some of them is created from QML files.



  • You have many ways to interact with QML.
    The two main ones are :

    A Context* myContext which is a QObject, which could be an object that keeps all the data you want to share with QML.
    and inside it have some properties like :
    @
    Q_PROPERTY(int progressBarDuration READ getProgressBarDuration WRITE setProgressBarDuration NOTIFY progressBarDurationChanged)@
    then with @yourDeclarativeView->rootContext()->setContextProperty("Context", myContext);@
    and inside
    you can access throught Context.progressBarDuration

    A QDeclarativeItem is rarely created in Cpp, but you could, however.
    You could also declare it, and then using in QML like any other component with some import.

    In your example i think the first approach may be better, ie using a kind of Context.logViewerManager.
    Then, if you have some lists, use setContextProperty(my model....

    However everything is in the doc :)



  • [quote author="dmcr" date="1352988312"]You have many ways to interact with QML.
    The two main ones are :

    A Context* myContext which is a QObject, which could be an object that keeps all the data you want to share with QML.
    and inside it have some properties like :
    @
    Q_PROPERTY(int progressBarDuration READ getProgressBarDuration WRITE setProgressBarDuration NOTIFY progressBarDurationChanged)@
    then with @yourDeclarativeView->rootContext()->setContextProperty("Context", myContext);@
    and inside
    you can access throught Context.progressBarDuration

    A QDeclarativeItem is rarely created in Cpp, but you could, however.
    You could also declare it, and then using in QML like any other component with some import.

    In your example i think the first approach may be better, ie using a kind of Context.logViewerManager.
    Then, if you have some lists, use setContextProperty(my model....

    However everything is in the doc :)
    [/quote]

    Thanks! It's exactly what I need! Thanks for the help!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.