Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Need help: Unable to assign [undefined] to QString



  • Hey, I am new to Qt and so i want to make a little program.

    A small table which outputs some data from a List.
    But i am not able to print the data. What do I do wrong?

    Thank you for helping!

    Main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "getdata.h"
    #include <QQmlContext>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        const char* ui = "GETDATA"; // @uri GETDATA
        qmlRegisterType <GetData> (ui, 1, 0, "GetData");
    
        GetData* getData = new GetData;
    
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("dataProvider", getData);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    getdata.h

    #ifndef GETDATA_H
    #define GETDATA_H
    
    #include <QList>
    #include <QObject>
    
    class GetData : public QObject
    {
        public:
            GetData();
    
            Q_PROPERTY(QString name READ getName)
            QString name = "x";
    
        public slots:
            QString getName();
    };
    
    #endif // GETDATA_H
    
    

    getdata.cpp

    #include "getdata.h"
    
    GetData::GetData()
    {
    }
    
    QString GetData::getName(){
        return name;
    }
    

    QML- Main File:

    import QtQuick 2.11
    import QtQuick.Window 2.11
    import GETDATA 1.0
    
    Window {
        id: root
        visible: true
        width: 840
        height: 480
        title: qsTr("Rennliste")
    
        GetData {
            id: m_data;
        }
    
        Column{
            id: finalListColumn
            spacing: 5
    
            RowItem{
                placeText: "Platzierung"
                pointsText: "Gesamtpunkte"
                nameText: "Name"
                fontSize: 12
            }
            Repeater{
                model: 3
                RowItem{
                    placeText: index
                    pointsText: ""
                    nameText: m_data.name
                    fontSize: 9
                }
            }
        }
    }
    

    QML - RowItem:

    import QtQuick 2.0
    
    Row {
        id: finalList
        spacing: 5
    
        property string placeText
        property string pointsText
        property string nameText
    
        property int fontSize
    
        Rectangle{
            id: place;
            width: 150;
            height: 30;
    
            Text {
                text: placeText;
                font.pointSize: fontSize;
            }
        }
        Rectangle{
            id: points;
            width: 150;
            height: 30;
            Text {
                text: pointsText;
                font.pointSize: fontSize;
            }
        }
        Rectangle{
            id: name;
            width: 150;
            height: 30;
            Text {
                text: nameText;
                font.pointSize: fontSize;
            }
        }
    }
    

  • Moderators

    Where does QML engine locate that error? Can you indicate that line in source you pasted?

    Issues I see are:

    • you create 2 separate GetData objects (one in QML m_data, one in C++ dataProvider), but use only one of them (the QML one)
    • your GetData is missing Q_OBJECT macro
    • getName() method is not const
    • name property is not marked as CONSTANT
    • GetData constructor does not initialize it's base class (QObject)
    • not all user-visible strings marked with qsTr()


  • Hey~
    i corrected the issues and now it works fine!
    Thank you so much!


Log in to reply