Jedi Engineer
Jedi Engineer

Reputation: 493

change text in QML textbox from C++ Qt 5.2.1

I'm a little new to QML and C++ in general, but I'm learning quick. I'm a bit stumped on something though. I'm trying to make an application that reads text from a QML TextInput text field, and writes the data to a text area. I'm not sure what is going wrong, but upon reading the text I get the error "Unable to assign QQuickTextInput to QString" - and I'm a bit stumped on how to write back to a text area. If someone could shed some light, that would be appreciated. I've already been through the QML/C++ binding page, and that doesn't seem to help. I'm wondering if that page was written for an older version of Qt... Here's my code thus far:

QML:

 import QtQuick 2.0
import QtMultimedia 5.0
import QtQuick.Controls 1.1
import QtQuick.Window 2.1
import QtWinExtras 1.0
import QtQuick.Layouts 1.1

Rectangle {
    id: main
    width: 640
    height: 480
    signal onClicked_button

    TextInput {
        id: textbocks
        objectName: textbocks
        x: 280
        y: 230
        width: 80
        height: 20
        text: "Text Input"
        font.pixelSize: 12

        Button {
            id: button1
            x: -10
            y: 45
            text: "Button"
            onClicked:main.onClicked_button()
        }
    }

    TextField {
        id: textField1
        x: 257
        y: 329
        placeholderText: qsTr("Text Field")
    }
}

header (class - originally designed to write back to text input):

#ifndef TEST_H
#define TEST_H

#include <QObject>
#include <QtCore>
#include <QString>
#include <QQuickView>
#include <QQuickItem>
#include <QQmlComponent> // for accessing QML from C++
#include <QQmlContext>   // for accessing C++ from QML

class test : public QObject
{
    Q_OBJECT
public:
    explicit test(QObject *parent = 0);

signals:

public slots:

    void test_button(){
        QQuickView *view = new QQuickView(QUrl("test.qml"));
        view->show();
        QQuickItem *item = view->rootObject();

        QObject *item = object->findChild<QObject*>("textbocks");
        textbox->setProperty("text", str);

        delete textbox;
        return;}
private:
    bool switched;
    bool textOutput;
    QString str = "Hello World";
};

#endif // TEST_H

And main .cpp file:

#include "test.h"
#include <QGuiApplication>
#include <QtCore>
#include <QObject>
#include <QQuickItem>
#include <QQmlComponent>
#include <QQmlContext>
#include <QQuickView>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeContext>


test::test(QObject *parent) :
    QObject(parent)
{
}


int main (int argc, char*argv[]) {


    QGuiApplication app(argc, argv);

    QQuickView *view = new QQuickView(QUrl("test.qml"));
    view->show();
    QQuickItem *item = view->rootObject();

    test *funcs = new test();

    // connect button signals to their slots:
    QObject::connect(item, SIGNAL(buttonClicked_button()), funcs, SLOT(test_button()));







    delete funcs;
    return 0;
}

Any help would be appreciated. Some of this is still a bit abstract for me... Thanks!!

Upvotes: 1

Views: 4654

Answers (2)

Redanium
Redanium

Reputation: 879

as I noticed you are creating two QQuickViews from the same QML file "test.qml" :

1.In the main function (main.cpp)

2.and in the TEST_BUTTON function (test.h)

which are different QObject . Another issues are that :

  • you are assigning an object of type TextInput to the objectName property (test.qml) which is a String

  • you are deleting (as Yekmen mentioned earlier) the object (textbox (test.h) = textbocks (test.qml)) which in the mean time you are assigning to it the string str="hello world" via setProperty() function . Ahh forgot one, the slot name in C++ side which must be identical to the one declared in QML

Upvotes: 2

yekmen
yekmen

Reputation: 127

I think you should look this method :

 Connections {
 target: area
 onClicked: foo(parameters)

}

For example : area is your c++ object.( qmlRegisterType(myclass) ) area send a signal and after you can lauch your function. I think is more easy.

Upvotes: 0

Related Questions