PDA

View Full Version : Program works normally but QML Reference Error



Bedopies
13th May 2019, 21:28
Hello, I have an program. It's probably work's normally but in "Application Output" I got some Reference Errors:



qrc:/main.qml:17: ReferenceError: Keppoqml is not defined
qrc:/main.qml:17: ReferenceError: Keppoqml is not defined




import QtQuick 2.9
import QtQuick.Window 2.2

Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")

TextInput {
id: textInput
objectName:"inputText"
x: 48
y: 58
width: 80
height: 20
text: Keppoqml.message // Here's the error.
font.pixelSize: 12
onTextEdited:{
Keppoqml.setWindowName(textInput.text);
}
}

Rectangle {
id: rectangle
x: 276
y: 41
width: 112
height: 56
color: "#e30a0a"

MouseArea {
id: mouseArea
x: 0
y: 0
width: 112
height: 56
onClicked:{
Siemaqml.check();
}
}
}

Rectangle {
id: rectangle1
x: 276
y: 180
width: 112
height: 62
color: "#29ff46"

MouseArea {
id: mouseArea1
x: 0
y: 0
width: 112
height: 62
onClicked:{
Siemaqml.load();
}
}
}

Rectangle {
id: rectangle2
x: 36
y: 227
width: 101
height: 106
color: "#000000"

MouseArea {
id: mouseArea2
x: 0
y: 0
width: 101
height: 106
onClicked:{
Keppoqml.checkload();
}
}
}
Rectangle {
id: rectangle5
x: 276
y: 324
width: 112
height: 62
color: "#29ff46"

MouseArea {
id: mouseArea5
x: 0
y: 0
width: 112
height: 62
onClicked:{
Keppoqml.message;
}
}
}
}



#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
#include <keppoxd.h>
#include <siema.h>
#include <QQmlContext>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{

QCoreApplication::setAttribute(Qt::AA_EnableHighDp iScaling);

QGuiApplication app(argc, argv);



siema siema;
keppoxd keppo;

keppo.siema = &siema;

QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);


QQmlContext *ctx = engine.rootContext();

ctx->setContextProperty("Siemaqml", &siema);
ctx->setContextProperty("Keppoqml", &keppo);

return app.exec();
}



#ifndef KEPPOXD_H
#define KEPPOXD_H

#include <QObject>
#include <QDebug>
#include <QSettings>
#include <QDir>
#include <siema.h>

class keppoxd : public QObject
{
Q_OBJECT
public:
explicit keppoxd(QObject *parent = nullptr);
siema *siema;

Q_PROPERTY(QString message READ message WRITE setWindowName NOTIFY textChanged)


signals:
void textChanged();
public slots:
QString message(){
qDebug() << siema->name;
return siema->name;
}
void setWindowName(QString value);
void checkload();

};

#endif // KEPPOXD_H



#include "keppoxd.h"

keppoxd::keppoxd(QObject *parent) : QObject(parent)
{
connect(this, SIGNAL (textChanged()), this, SLOT(message()));
}

void keppoxd::setWindowName(QString value)
{
siema->name = value;
qDebug() << "siema.name value from keppo class " << siema->name;
}

void keppoxd::checkload()
{
qDebug() << "keppoload " << siema->name;
emit textChanged();
}



#ifndef SIEMA_H
#define SIEMA_H

#include <QObject>
#include <QDebug>
#include <QSettings>
#include <QDir>
#include <QQuickView>


class siema : public QObject
{
Q_OBJECT


public:
explicit siema(QObject *parent = nullptr);

QString name = "old";

signals:
public slots:
void check();
void load();

};

#endif // SIEMA_H



#include "siema.h"

siema::siema(QObject *parent) : QObject(parent)
{
}

void siema::check()
{
qDebug() << "siema value " << name;

QSettings* settings;
settings = new QSettings(QDir::currentPath() + "/config.ini", QSettings::IniFormat);
settings->setValue("textfield", name);
settings->sync();
}
void siema::load(){
QSettings* settings;
settings = new QSettings(QDir::currentPath() + "/config.ini", QSettings::IniFormat);
name = settings->value("textfield").toString();
}

anda_skoa
15th May 2019, 20:03
You need to call setContextProperty() before you load the QML code.

Cheers,
_

Bedopies
15th May 2019, 20:20
I have it on my main file:



#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
#include <keppoxd.h>
#include <siema.h>
#include <QQmlContext>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{

QCoreApplication::setAttribute(Qt::AA_EnableHighDp iScaling);

QGuiApplication app(argc, argv);



siema siema;
keppoxd keppo;

keppo.siema = &siema;

QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);


QQmlContext *ctx = engine.rootContext();

ctx->setContextProperty("Siemaqml", &siema);
ctx->setContextProperty("Keppoqml", &keppo); // HERE

return app.exec();
}

d_stranz
16th May 2019, 19:23
Your code is loading the QML before it calls setContextProperty(). Read anda_skoa's reply again.