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.

main.qml

Rectangle{
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

TextInput
{
    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; 
        Qt.inputMethod.hide(); 
        textbox.qmlSignal(inputText.text); 
        console.log(colorText.color) }

 }
}

main.cpp

#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;
viewer.setMainQmlFile(QStringLiteral("qml/Test3/main.qml"));

QObject *item = viewer.rootObject();

MyClass test;

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

viewer.showExpanded();

return app.exec();
}

myclass.h

#include<QObject>
#include<QDebug>
#include<QtQuick>
#include"qtquick2applicationviewer.h"

class MyClass: public QObject
{
Q_OBJECT

public:
MyClass();

public slots:
void cppSlot(const QString &msg)
{
    qDebug() << "Called the C++ slot with message:" << msg;
    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/Test3/main.qml"));

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

Answers


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