[SOLVED]Q_PROPERTY QString linking qml and c++ giving "undefined" error



  • This simple example should test the linking between QObject class property and qml. A string from TextInput should get displayed in a Rectangle on a mouse click. Instead, an error - Cannot assign [undefined] to QString - is delivered. Could anyonw help?

    @//mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QDeclarativeView>
    #include <QObject>
    #include <QString>

    class MainWindow : public QDeclarativeView
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    };

    class Data : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString teamName READ getTeamName WRITE setTeamName NOTIFY nameChanged)

    public: Data(QObject *parent = 0);

    QString getTeamName() const;
    void setTeamName(const QString &newString);
    

    signals:
    void nameChanged(QString &);

    private:
    QString newTeam;
    };

    #endif // MAINWINDOW_H@

    @//mainwindow.cpp
    #include "mainwindow.h"
    #include <QDeclarativeView>
    #include <QObject>
    #include <QString>

    MainWindow::MainWindow(QWidget *parent)
    : QDeclarativeView(parent)
    {

    }

    MainWindow::~MainWindow()
    {

    }

    Data::Data(QObject *parent) : QObject(parent)
    {

    }

    QString Data::getTeamName() const{
    return newTeam;
    }

    void Data::setTeamName(const QString &newString){
    if(newString != newTeam)
    {
    newTeam = newString;
    emit nameChanged(newTeam);
    }
    }@

    @//main.cpp
    #include "mainwindow.h"
    #include "qdeclarative.h"
    #include <QApplication>
    #include <QDeclarativeView>
    #include <QDeclarativeContext>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    qmlRegisterType<Data>("NData", 1, 0, "Data");
    w.setSource(QUrl::fromLocalFile("../subor1/Main.qml"));

    Data d;
    w.rootContext()->setContextProperty("data", &d);
    
    w.show();
    
    return a.exec(&#41;;
    

    }@

    @//Main.qml
    import QtQuick 1.1
    import NData 1.0

    Rectangle {
    id: root

    Rectangle{
        id: textIn
        ...
        TextInput{
            id: tIn
            text: ""
        }
    }
    Rectangle{
        id: textOut
        ...
        Text{
            id: tOut
            anchors.centerIn: textOut
            text: ""
        }
    }
    Rectangle{
        id: textClick
        ...
        Text{
            id: tcText
            anchors.centerIn: textClick
            text: "Click"
        }
        MouseArea{
            id: tcMouse
            anchors.fill: textClick
            onClicked: {
                data.teamName = tIn.text
                tOut.text = data.teamName
            }
        }
    }
    

    }@



  • "data":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-item.html#data-prop is one of the properties of the qml "Item":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-item.html component.

    Try changing this:

    @
    w.rootContext()->setContextProperty("data", &d);
    @

    to this:

    @
    w.rootContext()->setContextProperty("dataProvider", &d);
    @

    Also, make sure you set your context property before creating your scene.

    @
    Data d;
    // You must do this....
    w.rootContext()->setContextProperty("dataProvider", &d);

    // before doing this
    w.setSource(QUrl::fromLocalFile("../subor1/Main.qml"));
    @

    Also, if you do not need to create an instance of your Data class from qml then maybe you should change this:

    @
    qmlRegisterType<Data>("NData", 1, 0, "Data");
    @

    to this:

    @
    qmlRegisterType<Data>();
    @

    Also, if the purpose of your Data class is to export data from c++ to qml only then have a look at the solution posted "here":http://qt-project.org/forums/viewthread/26191/. It shows you how to create a singleton class that is used to export data from c++ to qml. There is both a QQuick 1.0 and 2.0 solution.



  • Thanks a lot! It really works as supposed now. Thank You.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.