I try to implement your suggestion but I keep having issues loading the qml.
Component {
id: networkManager
url: "../network/NetworkManagerQml.qml"
}
Component {
id: networkManager
url: "../network/NetworkManagerQml.qml"
}
To copy to clipboard, switch view to plain text mode
I get this error:
Component elements may not contain properties other than id
I have also tried this:
...
MouseArea {
id: maRename
anchors.fill: parent
onClicked: {
var component = Qt.createComponent("../network/NetworkManagerQml.qml");
if (component.status == Component.Ready) {
var obj = component.createObject(myText);
if (null == obj)
console.log("Error creating component: " + component.errorString());
else
{
obj.ready.connect(function(text){ myText.text = text; obj.destroy(); });
obj.fetch("http://www.google.com");
}
}
else
console.log("Error loading component " + component.errorString());
}
}
...
MouseArea {
id: maRename
anchors.fill: parent
onClicked: {
var component = Qt.createComponent("../network/NetworkManagerQml.qml");
if (component.status == Component.Ready) {
var obj = component.createObject(myText);
if (null == obj)
console.log("Error creating component: " + component.errorString());
else
{
obj.ready.connect(function(text){ myText.text = text; obj.destroy(); });
obj.fetch("http://www.google.com");
}
}
else
console.log("Error loading component " + component.errorString());
}
}
To copy to clipboard, switch view to plain text mode
I get this error:
Error loading component qrc:/component/network/NetworkManagerQml.qml: 13 Cannot assign to non-existaent default property.
If it helps my class looks like this:
#ifndef NETWORKMANAGER_H
#define NETWORKMANAGER_H
#include <QObject>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
class NetworkManager
: public QObject{
Q_OBJECT
public:
Q_INVOKABLE void fetch(const QString& url);
public slots:
void replyFinished(QNetworkReply*);
signals:
void reply(const QString& reply);
private:
QNetworkAccessManager* m_NetworkManager;
};
#endif // NETWORKMANAGER_H
#ifndef NETWORKMANAGER_H
#define NETWORKMANAGER_H
#include <QObject>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
class NetworkManager : public QObject
{
Q_OBJECT
public:
NetworkManager(QObject *parent=0);
Q_INVOKABLE void fetch(const QString& url);
public slots:
void replyFinished(QNetworkReply*);
signals:
void reply(const QString& reply);
private:
QNetworkAccessManager* m_NetworkManager;
};
#endif // NETWORKMANAGER_H
To copy to clipboard, switch view to plain text mode
my NetworkManagerQml.qml
import QtQuick 2.4
import MoeNetworkManager 1.0
QtObject {
id: manager
signal ready(string text)
function fetch(url) {
networkManager.fetch(url);
}
NetworkManagerHandler {
id: networkManager
onReply: {
manager.ready(text);
}
}
}
import QtQuick 2.4
import MoeNetworkManager 1.0
QtObject {
id: manager
signal ready(string text)
function fetch(url) {
networkManager.fetch(url);
}
NetworkManagerHandler {
id: networkManager
onReply: {
manager.ready(text);
}
}
}
To copy to clipboard, switch view to plain text mode
in my main.cpp
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<NetworkManager>("MoeNetworkManager", 1, 0, "NetworkManagerHandler");
QQmlApplicationEngine engine;
engine.
load(QUrl(QStringLiteral
("qrc:/component/main.qml")));
return app.exec();
}
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<NetworkManager>("MoeNetworkManager", 1, 0, "NetworkManagerHandler");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/component/main.qml")));
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Any idea whats going wrong?
Added after 1 43 minutes:
Sorry about the stupid question... Sometimes I don't get what I read. I have some dyslexia. In your example QtObject was meant to be a general component. I thought it actually has to be of the type QtObject. If I change it to e.g. Item it all works as expected. I am back on track now doing some more testing
The second snippet from above actually works. However, this
Component {
id: networkManager
url: "../network/NetworkManagerQml.qml"
}
Component {
id: networkManager
url: "../network/NetworkManagerQml.qml"
}
To copy to clipboard, switch view to plain text mode
doesn't work as expected. See error above. But that's ok I can load the object in a function.
Bookmarks