Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] How to call refresh method(s) in QML/C++
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 2 Posters 22.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shav
    wrote on 14 Nov 2012, 18:00 last edited by
    #1

    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!

    Mac OS and iOS Developer

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on 15 Nov 2012, 10:08 last edited by
      #2

      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!

      dmcr

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shav
        wrote on 15 Nov 2012, 13:37 last edited by
        #3

        [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.

        Mac OS and iOS Developer

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dmcr
          wrote on 15 Nov 2012, 14:05 last edited by
          #4

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

          dmcr

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shav
            wrote on 15 Nov 2012, 16:12 last edited by
            #5

            [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!

            Mac OS and iOS Developer

            1 Reply Last reply
            0

            1/5

            14 Nov 2012, 18:00

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved