Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED]Q_PROPERTY QString linking qml and c++ giving "undefined" error
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 8.1k 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.
  • B Offline
    B Offline
    Billcode
    wrote on 22 Apr 2013, 05:39 last edited by
    #1

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

    }@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      melghawi
      wrote on 22 Apr 2013, 11:59 last edited by
      #2

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

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Billcode
        wrote on 22 Apr 2013, 16:40 last edited by
        #3

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

        1 Reply Last reply
        0

        1/3

        22 Apr 2013, 05:39

        • 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