Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QML, QtQuick - modify text object from C++ code

QML, QtQuick - modify text object from C++ code

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.6k 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 17 Sept 2013, 04:21 last edited by
    #1

    I have done a lot of research but I failed to find such simple example. I'm obviously searching wrong thing because I cannot imagine that modifying a GUI text from code behind is very complex.

    I have read the "QtBindings page":http://qt-project.org/doc/qt-4.8/qtbinding.html and the example included on this page (shown below) but was unable to apply it to my embedded Qt project which uses QDeclarativeView to interface with LCD.
    @ // main.cpp
    QDeclarativeEngine engine;
    QDeclarativeComponent component(&engine, "MyItem.qml");
    QObject *object = component.create();

    QVariant returnedValue;
    QVariant msg = "Hello from C++";
    QMetaObject::invokeMethod(object, "myQmlFunction",
    Q_RETURN_ARG(QVariant, returnedValue),
    Q_ARG(QVariant, msg));

    qDebug() << "QML function returned:" << returnedValue.toString();
    delete object;@

    So here is a simplified version of my code. Could someone tell me if it is possible for the QML to detect a change value of the QString variable when a setText() is invoked. I am guessing that I need a connect statement which will bind my textChanged() signal with the QML. I also was not able to find such example.
    How would I define a slot in QML and for what object? In which file should I place this connect statement? I am really looking for syntax here...

    I am also aware of findChild way of locating objects in QML but apparently this is not a good way of doing such task in Qt per the same qt-project page I have mentioned above.

    This code works, but the second setText("Change 2") does not update screen. The screen is only updated once when the setContextProperty line is executed.

    @// main.cpp
    #include <QApplication>
    #include "qmlapplicationviewer.h"
    #include "mydisplay.h"

    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QDeclarativeView view;
    MyDisplay display;

    // This setText updates QML
    display.setText("Change 1");
    
    view.setSource(QUrl::fromLocalFile&#40;"qml/qmlTest1/main.qml"&#41;);
    view.rootContext()->setContextProperty("myDisplay", &display);
    
    // This setText does not update QML
    display.setText("Change 2");
    // Debug console displayes "Change 2" string correctly.
    qDebug() << display.getText();
    view.show();
    
    return app.exec();
    

    }
    @
    @//mydisplay.h
    #include <QObject>
    #include <QtDeclarative>

    #ifndef MYDISPLAY_H
    #define MYDISPLAY_H

    class MyDisplay : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString newText READ getText WRITE setText NOTIFY textChanged)

    public:
    MyDisplay();

    MyDisplay(QString);
    
    Q_INVOKABLE QString getText() const;
    

    public slots:
    void setText(QString text);

    signals:
    void textChanged(QString);

    private:
    QString newText;
    };

    #endif // MYDISPLAY_H@

    @//mydisplay.cpp
    #include "mydisplay.h"

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

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

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

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

    @// main.qml
    import QtQuick 1.1

    Rectangle {
    width: 800
    height: 480
    color: "#000000"

    Text {
        id: myQmlTextObject
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        color: "#b9b9b9"
    
        font.pixelSize: 30
        text: myDisplay.getText();
    }
    

    }@
    Working code would be greatly appreciated.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 17 Sept 2013, 06:29 last edited by
      #2

      @
      // in QML
      text: myDisplay.newText
      @

      You need to bind to the property, not it's getter method.

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        marcin100
        wrote on 18 Sept 2013, 02:29 last edited by
        #3

        Thank you!
        I knew this will be something obvious.

        1 Reply Last reply
        0

        1/3

        17 Sept 2013, 04:21

        • Login

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