Results 1 to 4 of 4

Thread: Program works normally but QML Reference Error

  1. #1
    Join Date
    May 2019
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Program works normally but QML Reference Error

    Hello, I have an program. It's probably work's normally but in "Application Output" I got some Reference Errors:

    Qt Code:
    1. qrc:/main.qml:17: ReferenceError: Keppoqml is not defined
    2. qrc:/main.qml:17: ReferenceError: Keppoqml is not defined
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. import QtQuick 2.9
    2. import QtQuick.Window 2.2
    3.  
    4. Window {
    5. visible: true
    6. width: 640
    7. height: 480
    8. title: qsTr("Hello World")
    9.  
    10. TextInput {
    11. id: textInput
    12. objectName:"inputText"
    13. x: 48
    14. y: 58
    15. width: 80
    16. height: 20
    17. text: Keppoqml.message // Here's the error.
    18. font.pixelSize: 12
    19. onTextEdited:{
    20. Keppoqml.setWindowName(textInput.text);
    21. }
    22. }
    23.  
    24. Rectangle {
    25. id: rectangle
    26. x: 276
    27. y: 41
    28. width: 112
    29. height: 56
    30. color: "#e30a0a"
    31.  
    32. MouseArea {
    33. id: mouseArea
    34. x: 0
    35. y: 0
    36. width: 112
    37. height: 56
    38. onClicked:{
    39. Siemaqml.check();
    40. }
    41. }
    42. }
    43.  
    44. Rectangle {
    45. id: rectangle1
    46. x: 276
    47. y: 180
    48. width: 112
    49. height: 62
    50. color: "#29ff46"
    51.  
    52. MouseArea {
    53. id: mouseArea1
    54. x: 0
    55. y: 0
    56. width: 112
    57. height: 62
    58. onClicked:{
    59. Siemaqml.load();
    60. }
    61. }
    62. }
    63.  
    64. Rectangle {
    65. id: rectangle2
    66. x: 36
    67. y: 227
    68. width: 101
    69. height: 106
    70. color: "#000000"
    71.  
    72. MouseArea {
    73. id: mouseArea2
    74. x: 0
    75. y: 0
    76. width: 101
    77. height: 106
    78. onClicked:{
    79. Keppoqml.checkload();
    80. }
    81. }
    82. }
    83. Rectangle {
    84. id: rectangle5
    85. x: 276
    86. y: 324
    87. width: 112
    88. height: 62
    89. color: "#29ff46"
    90.  
    91. MouseArea {
    92. id: mouseArea5
    93. x: 0
    94. y: 0
    95. width: 112
    96. height: 62
    97. onClicked:{
    98. Keppoqml.message;
    99. }
    100. }
    101. }
    102. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QGuiApplication>
    2. #include <QQmlApplicationEngine>
    3. #include <QDebug>
    4. #include <keppoxd.h>
    5. #include <siema.h>
    6. #include <QQmlContext>
    7. #include <QQmlApplicationEngine>
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11.  
    12. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    13.  
    14. QGuiApplication app(argc, argv);
    15.  
    16.  
    17.  
    18. siema siema;
    19. keppoxd keppo;
    20.  
    21. keppo.siema = &siema;
    22.  
    23. QQmlApplicationEngine engine;
    24. const QUrl url(QStringLiteral("qrc:/main.qml"));
    25. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
    26. &app, [url](QObject *obj, const QUrl &objUrl) {
    27. if (!obj && url == objUrl)
    28. QCoreApplication::exit(-1);
    29. }, Qt::QueuedConnection);
    30. engine.load(url);
    31.  
    32.  
    33. QQmlContext *ctx = engine.rootContext();
    34.  
    35. ctx->setContextProperty("Siemaqml", &siema);
    36. ctx->setContextProperty("Keppoqml", &keppo);
    37.  
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef KEPPOXD_H
    2. #define KEPPOXD_H
    3.  
    4. #include <QObject>
    5. #include <QDebug>
    6. #include <QSettings>
    7. #include <QDir>
    8. #include <siema.h>
    9.  
    10. class keppoxd : public QObject
    11. {
    12. Q_OBJECT
    13. public:
    14. explicit keppoxd(QObject *parent = nullptr);
    15. siema *siema;
    16.  
    17. Q_PROPERTY(QString message READ message WRITE setWindowName NOTIFY textChanged)
    18.  
    19.  
    20. signals:
    21. void textChanged();
    22. public slots:
    23. QString message(){
    24. qDebug() << siema->name;
    25. return siema->name;
    26. }
    27. void setWindowName(QString value);
    28. void checkload();
    29.  
    30. };
    31.  
    32. #endif // KEPPOXD_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "keppoxd.h"
    2.  
    3. keppoxd::keppoxd(QObject *parent) : QObject(parent)
    4. {
    5. connect(this, SIGNAL (textChanged()), this, SLOT(message()));
    6. }
    7.  
    8. void keppoxd::setWindowName(QString value)
    9. {
    10. siema->name = value;
    11. qDebug() << "siema.name value from keppo class " << siema->name;
    12. }
    13.  
    14. void keppoxd::checkload()
    15. {
    16. qDebug() << "keppoload " << siema->name;
    17. emit textChanged();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef SIEMA_H
    2. #define SIEMA_H
    3.  
    4. #include <QObject>
    5. #include <QDebug>
    6. #include <QSettings>
    7. #include <QDir>
    8. #include <QQuickView>
    9.  
    10.  
    11. class siema : public QObject
    12. {
    13. Q_OBJECT
    14.  
    15.  
    16. public:
    17. explicit siema(QObject *parent = nullptr);
    18.  
    19. QString name = "old";
    20.  
    21. signals:
    22. public slots:
    23. void check();
    24. void load();
    25.  
    26. };
    27.  
    28. #endif // SIEMA_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "siema.h"
    2.  
    3. siema::siema(QObject *parent) : QObject(parent)
    4. {
    5. }
    6.  
    7. void siema::check()
    8. {
    9. qDebug() << "siema value " << name;
    10.  
    11. QSettings* settings;
    12. settings = new QSettings(QDir::currentPath() + "/config.ini", QSettings::IniFormat);
    13. settings->setValue("textfield", name);
    14. settings->sync();
    15. }
    16. void siema::load(){
    17. QSettings* settings;
    18. settings = new QSettings(QDir::currentPath() + "/config.ini", QSettings::IniFormat);
    19. name = settings->value("textfield").toString();
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Program works normally but QML Reference Error

    You need to call setContextProperty() before you load the QML code.

    Cheers,
    _

  3. #3
    Join Date
    May 2019
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Program works normally but QML Reference Error

    I have it on my main file:

    Qt Code:
    1. #include <QGuiApplication>
    2. #include <QQmlApplicationEngine>
    3. #include <QDebug>
    4. #include <keppoxd.h>
    5. #include <siema.h>
    6. #include <QQmlContext>
    7. #include <QQmlApplicationEngine>
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11.  
    12. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    13.  
    14. QGuiApplication app(argc, argv);
    15.  
    16.  
    17.  
    18. siema siema;
    19. keppoxd keppo;
    20.  
    21. keppo.siema = &siema;
    22.  
    23. QQmlApplicationEngine engine;
    24. const QUrl url(QStringLiteral("qrc:/main.qml"));
    25. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
    26. &app, [url](QObject *obj, const QUrl &objUrl) {
    27. if (!obj && url == objUrl)
    28. QCoreApplication::exit(-1);
    29. }, Qt::QueuedConnection);
    30. engine.load(url);
    31.  
    32.  
    33. QQmlContext *ctx = engine.rootContext();
    34.  
    35. ctx->setContextProperty("Siemaqml", &siema);
    36. ctx->setContextProperty("Keppoqml", &keppo); // HERE
    37.  
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Program works normally but QML Reference Error

    Your code is loading the QML before it calls setContextProperty(). Read anda_skoa's reply again.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. error: undefined reference to. Where?
    By remizero in forum Installation and Deployment
    Replies: 4
    Last Post: 8th April 2017, 19:06
  2. Replies: 1
    Last Post: 23rd April 2014, 11:03
  3. Replies: 4
    Last Post: 27th August 2013, 16:34
  4. Replies: 5
    Last Post: 16th December 2010, 11:12
  5. Program works in Release but not Debug
    By Ferric in forum Newbie
    Replies: 2
    Last Post: 28th January 2010, 02:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.