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. Need help: Unable to assign [undefined] to QString

Need help: Unable to assign [undefined] to QString

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 5 Posters 12.4k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    rylicx
    wrote on last edited by
    #1

    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;
            }
        }
    }
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by sierdzio
      #2

      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()

      (Z(:^

      1 Reply Last reply
      3
      • R Offline
        R Offline
        rylicx
        wrote on last edited by
        #3

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

        J 1 Reply Last reply
        1
        • R rylicx

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

          J Offline
          J Offline
          Jaswenth S
          wrote on last edited by
          #4

          @rylicx
          hi buddy
          I am beginner in Qt and I am facing the same error like "Unable to assign [undefined] to int"
          and while printing that in console I get a garbage value like "-842150451".
          I was stuck here , help me through this

          jsulmJ 1 Reply Last reply
          0
          • J Jaswenth S

            @rylicx
            hi buddy
            I am beginner in Qt and I am facing the same error like "Unable to assign [undefined] to int"
            and while printing that in console I get a garbage value like "-842150451".
            I was stuck here , help me through this

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Jaswenth-S Please post the relevant code.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            J 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Jaswenth-S Please post the relevant code.

              J Offline
              J Offline
              Jaswenth S
              wrote on last edited by
              #6

              @jsulm Thanks for your time , I solved it bro and sorry for the late reply

              K 1 Reply Last reply
              0
              • J Jaswenth S

                @jsulm Thanks for your time , I solved it bro and sorry for the late reply

                K Offline
                K Offline
                K Pradeep Kumar Reddy
                wrote on last edited by
                #7

                @Jaswenth-S How did you solve it ?

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved