QML/C++ Change Property During Run-time

I am trying to setup my program to take data from an input field in QML and then pass that data to C++, which will be used to make in a property change. For example if the user types red in the input field the rectangle containing the input field should turn red. The data is being received in C++ but the properties are not changing on the rectangle.

Here is my code. Any help is appreciated.


id: textbox
radius: 15.0
height: 300
width: 300
color: "white"
border.color: "lightblue"
border.width: 5
signal qmlSignal(string msg)
property alias textColor: colorText.color

    id: inputText
    anchors.horizontalCenter: textbox.horizontalCenter
    anchors.verticalCenter: textbox.verticalCenter
    anchors.bottomMargin: 25
    color : "black"
    text : "type something..."
    font.pointSize: 20
    maximumLength: 17
    inputMethodHints: Qt.ImhNoPredictiveText
    selectByMouse: true

    onAccepted: { inputText.focus = false; 
        console.log(colorText.color) }



#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QtQuick>
#include <QObject>
#include <myclass.h>

int main(int argc, char *argv[])
QGuiApplication app(argc, argv);

QtQuick2ApplicationViewer viewer;

QObject *item = viewer.rootObject();

MyClass test;

QObject::connect(item, SIGNAL(qmlSignal(QString)), &test, SLOT(cppSlot(QString)));


return app.exec();



class MyClass: public QObject


public slots:
void cppSlot(const QString &msg)
    qDebug() << "Called the C++ slot with message:" << msg;
    QtQuick2ApplicationViewer viewer;

    QObject *item = viewer.rootObject();
    item->setProperty("color", "red");


To solve your problem I believe you'll want to review the lessons on extending QML with bindings. Specifically see Chapter 3: Adding Property Bindings as this shows how to create bindings between C++ objects and QML using the Q_PROPERTY macro.

In fact I would highly recommend you complete all the chapters for QML that come with the Qt installation of Qt Creator. The tutorial chapters can be access via the Welcome Page

  1. Select Examples
  2. Enter "Chapter" in the search
  3. The six chapters should be listed

Need Your Help

SQL Server: Create a duplicate of a database without the data

sql sql-server toad

I have a database called AQOA_Core with huge amount of data.

How to open a directory to stream over it

c# file file-upload

I have built an application and a service that can stream files between each other, but it seems that my code, which supposed to read directories on flash drives and CDs, only works on flash drives...