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. Control/update the Qt Quick QML text element from C++
QtWS25 Last Chance

Control/update the Qt Quick QML text element from C++

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 3 Posters 6.7k Views
  • 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.
  • M Offline
    M Offline
    marcin100
    wrote on last edited by
    #1

    Seems like a very common task, yet I was not able to locate a simple example. I come from the Visual C environment were updating a text element is as simple as Text1.String = "My text string";
    Unfortunately the Qt/Qt Quick does not offer such a simple solution or does it?

    My goal is to have a dynamic (run time) text updates based on the activity within the app and Timer events (I got timer working already).

    Thank you for any suggestions. Complete examples are greatly appreciated since I am very new to Qt.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jens
      wrote on last edited by
      #2

      The thing is that you dont usually set the value of text elements directly in c++. Instead, you declare a string property, modify that and bind your text property to that on the qml side.

      This link contains a complete example:
      http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html

      In particular notice how the author property is exposed on the message object and how it is bound to the text element in QML.

      That said it is certainly possible (and should perhaps be more convenient) to obtain a pointer to the text object in C++ and call setProperty directly on it, but it is generally better to keep the UI layer separate.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        marcin100
        wrote on last edited by
        #3

        Sorry for a long delay... Thx, this is very good example but I am having hard time getting it to run.

        I use Qt 4.8 and not 5.0 and I do not get QQmlEngine (unless there is a specific #include which I am missing)

        I tried the following, but do not get anything on screen
        I only get this output:
        Qml debugging is enabled. Only use this in a safe environment!
        QDeclarativeComponent: Component is not ready
        @ QCoreApplication app(argc,argv);

        QDeclarativeEngine engine;
        Display displayTime;
        engine.rootContext()->setContextProperty("displayTime", &displayTime);
        QDeclarativeComponent component(&engine, QUrl::fromLocalFile("qml/QtPlayer3/main.qml"));
        component.create();
        

        return app.exec();@

        Finally do you know the difference between:
        QDeclarativeEngine
        QQmlEngine
        QmlApplicationViewer - this is the only way I am getting output on the screen. Here is the code:

        @ QScopedPointer<QApplication> app(createApplication(argc, argv));
        QmlApplicationViewer viewer;
        Display displayTime;
        viewer.rootContext()->setContextProperty("displayTime", &displayTime);
        viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
        viewer.setMainQmlFile(QLatin1String("qml/QtPlayer3/main.qml"));
        viewer.showExpanded();
        return app->exec();@

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          Hi,

          Qt 4.8 uses Qt Quick 1, while Qt 5 uses Qt Quick 2.

          In Qt Quick 1, all C++ class names started with QDeclarative*. In Qt Quick 2, those classes have been renamed to QQuick* or QQml* for better modularization, but they still do roughly the same jobs. See "here":http://qt-project.org/doc/qt-5.1/qtdoc/qtquick-porting-qt5.html#c-code for more details.

          Anyway, you can find examples for Qt 4.8 at http://qt-project.org/doc/qt-4.8/qtbinding.html

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • M Offline
            M Offline
            marcin100
            wrote on last edited by
            #5

            Thank you,
            I did this exactly as you described(used QDeclarativeEngine.) (shown in the first code example) but get no screen activity. I think the target board crashes because I loose link with QtCreator in this mode.

            When configured with QmlApplicationViewer instead of QDeclarativeEngine then GUI works fine but Signals do not seem to work.

            I am also not 100% sure that I have configured the signals and slots correctly.
            Where should the connect statement reside? main.cpp

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              Hi,

              [quote author="marcin100" date="1378562239"]...get no screen activity. I think the target board crashes because I loose link with QtCreator in this mode.[/quote]I recommend ensuring that the program works on your host PC first, before trying to deploy it to an embedded device. What board is it?

              [quote]When configured with QmlApplicationViewer instead of QDeclarativeEngine then GUI works fine but Signals do not seem to work.

              I am also not 100% sure that I have configured the signals and slots correctly.
              Where should the connect statement reside? main.cpp[/quote]Do you mean QML signals or C++ signals? Connected to QML slots or C++ slots?

              Please show us your code; it will make it easier to see what you're trying and to spot errors.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • M Offline
                M Offline
                marcin100
                wrote on last edited by
                #7

                I think I will stick to QmlApplicationViewer unless someone will tell me this is not a right way.

                Board: i.mx6 by Boundary Devices

                I did try runing it on PC with same result.

                I am almost certain that I need to connect the GUI text object with the MyDisplay object in C++ code, however I am stuck with syntax. I am also not sure in which part of the code should the "connect" statement be placed.

                Here is the main.cpp. The displayTime.setText("XXXXXXXX"); works correctly because the next line spits out the right output on the debug console. However the QML text object is still blank.

                @int main(int argc, char * argv[])
                {
                QScopedPointer<QApplication> app(createApplication(argc, argv));
                QmlApplicationViewer viewer;
                MyPlayer player(&viewer);
                MyDisplay displayTime;
                player.setDisplay(&displayTime);

                viewer.rootContext()->setContextProperty("myQmlTest", &player);
                viewer.rootContext()->setContextProperty("displayTime", &displayTime);
                viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
                viewer.setMainQmlFile&#40;QLatin1String("qml/QtPlayer3/main.qml"&#41;);
                viewer.showExpanded();
                viewer.showFullScreen();
                
                player.myGSTint();  // init GStreamer
                
                displayTime.setText("XXXXXXXX");
                qDebug() << "This is a test: " << displayTime.getText();
                
                return app->exec&#40;&#41;;
                

                }@

                ...and the QML section
                @
                Text
                {
                id: time
                //text: "none"
                color: "#999999"
                font.pixelSize: 30
                anchors.topMargin: 90
                anchors.top: parent.top
                anchors.horizontalCenter: parent.horizontalCenter
                text: displayTime.getText()
                }@

                and the MyDisplay class

                @MyDisplay::MyDisplay()
                {
                newText = "";
                }

                MyDisplay::MyDisplay(QString text)
                {
                newText = text;
                }

                QString MyDisplay::getText() const
                {
                return newText;
                }

                void MyDisplay::setText(const QString &text)
                {
                if (text != newText)
                {
                newText = text;
                emit textChanged(text);
                }
                }@

                1 Reply Last reply
                0

                • Login

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