Here you can find the source code of the example:
SourceCode_QML_Cpp.zip
Hello,
I have the following example - a QList of Nodes in ConfigurationModel .
I want to bind the properties "QString nodeID" and "QString iconFilePath" beween the QML component Node and the C++ class Node.
Unfortunetly the binding is not working - when you create the Node instances from C++, the properties of the correspondingly generated QML objects don't have the given values. They are empty. I cannot find where the issue comes from ... Could you please advise me, what I do wrong ?
main.qml
import QtQuick 1.0
Rectangle {
width: 500
height: 500
Repeater {
anchors.fill: parent
model: configModel
delegate: Node {}
}
}
import QtQuick 1.0
Rectangle {
width: 500
height: 500
Repeater {
anchors.fill: parent
model: configModel
delegate: Node {}
}
}
To copy to clipboard, switch view to plain text mode
Node.qml
import QtQuick 1.0
import NodeLib 1.0
Node {
id: nodeDelegate
Image{
id : nodeIcon
source: nodeDelegate.iconFilePath
}
Text {
anchors.top: nodeIcon.bottom
text: nodeDelegate.nodeID
}
MouseArea {
anchors.fill: parent
}
}
import QtQuick 1.0
import NodeLib 1.0
Node {
id: nodeDelegate
Image{
id : nodeIcon
source: nodeDelegate.iconFilePath
}
Text {
anchors.top: nodeIcon.bottom
text: nodeDelegate.nodeID
}
MouseArea {
anchors.fill: parent
}
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include "qmlapplicationviewer.h"
#include <QDeclarativeContext>
#include <QtDeclarative>
#include "ConfigurationModel.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QDeclarativeView declarativeView;
qmlRegisterType<Node>("NodeLib", 1, 0, "Node");
ConfigurationModel* p_configModel = ConfigurationModel::GetConfigModelInstance();
p_configModel->addNodeInConfigurationModel(new Node("PanelXXXX", PLUG_IN, EVALUATION_BLOCK, "./ui-images/cutetube.png"));
p_configModel->addNodeInConfigurationModel(new Node("PanelYYYY", PLUG_IN, EVALUATION_BLOCK, "./ui-images/cutetube.png"));
Node *p_nodeZ = new Node("PanelZZZZ", PLUG_IN, EVALUATION_BLOCK, "./ui-images/cutetube.png");
p_nodeZ->setNodeID("NODE ZZZZ");
p_configModel->addNodeInConfigurationModel(p_nodeZ);
// if the configuration has been changed, you need to call setContextProperty() again to update the QML view
declarativeView.rootContext()->setContextProperty("configModel", ConfigurationModel::GetConfigModelInstance());
declarativeView.
setSource(QUrl::fromLocalFile("qml/ConfigurationView/main.qml"));
declarativeView.show();
return app->exec();
}
#include <QApplication>
#include "qmlapplicationviewer.h"
#include <QDeclarativeContext>
#include <QtDeclarative>
#include "ConfigurationModel.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QDeclarativeView declarativeView;
qmlRegisterType<Node>("NodeLib", 1, 0, "Node");
ConfigurationModel* p_configModel = ConfigurationModel::GetConfigModelInstance();
p_configModel->addNodeInConfigurationModel(new Node("PanelXXXX", PLUG_IN, EVALUATION_BLOCK, "./ui-images/cutetube.png"));
p_configModel->addNodeInConfigurationModel(new Node("PanelYYYY", PLUG_IN, EVALUATION_BLOCK, "./ui-images/cutetube.png"));
Node *p_nodeZ = new Node("PanelZZZZ", PLUG_IN, EVALUATION_BLOCK, "./ui-images/cutetube.png");
p_nodeZ->setNodeID("NODE ZZZZ");
p_configModel->addNodeInConfigurationModel(p_nodeZ);
// if the configuration has been changed, you need to call setContextProperty() again to update the QML view
declarativeView.rootContext()->setContextProperty("configModel", ConfigurationModel::GetConfigModelInstance());
declarativeView.setSource(QUrl::fromLocalFile("qml/ConfigurationView/main.qml"));
declarativeView.show();
return app->exec();
}
To copy to clipboard, switch view to plain text mode
Node.h
#ifndef NODE_H
#define NODE_H
#include <QString>
#include <QList>
#include <QDeclarativeItem>
#include "GlobalDeclarations.h"
// Inherit from QDeclarativeItem in order to override paint() method and to display links to parent nodes
class Node : public QDeclarativeItem
{
Q_OBJECT
Q_PROPERTY(QString iconFilePath READ getIconFilePath WRITE setIconFilePath NOTIFY iconFilePathChanged
) Q_PROPERTY(QString nodeID READ getNodeID WRITE setNodeID NOTIFY nodeIDchanged
)
public:
// Constructors
Node();
Node
(QString nodeID, NodeType nodeType, PlugInType plugInType,
QString iconFilePath
);
QHash<int, QByteArray> roleNames() const;
// Destructor
~Node();
Q_INVOKABLE
bool setNodeID
(const QString &nodeID
);
Q_INVOKABLE
bool setIconFilePath
(const QString &iconFilePath
);
void hide();
void show();
void connectTo(Node* p_toNode);
void disconnectFrom(Node* p_toNode);
void removeNode();
signals:
void iconFilePathChanged
(QString data
);
void dataChanged();
protected:
void addParent(Node* p_parentNode);
const QList<Node*>& getParents() const;
QList<Node*>& accessParents();
void addChild(Node* p_childNode);
const QList<Node*>& getChildren() const;
QList<Node*>& accessChildren();
private:
NodeType _nodeType;
PlugInType _plugInType;
QList<Node*> _parents;
QList<Node*> _children;
};
#endif // NODE_H
#ifndef NODE_H
#define NODE_H
#include <QString>
#include <QList>
#include <QDeclarativeItem>
#include "GlobalDeclarations.h"
// Inherit from QDeclarativeItem in order to override paint() method and to display links to parent nodes
class Node : public QDeclarativeItem
{
Q_OBJECT
Q_PROPERTY(QString iconFilePath READ getIconFilePath WRITE setIconFilePath NOTIFY iconFilePathChanged)
Q_PROPERTY(QString nodeID READ getNodeID WRITE setNodeID NOTIFY nodeIDchanged)
public:
// Constructors
Node();
Node(QString nodeID, NodeType nodeType, PlugInType plugInType, QString iconFilePath);
QHash<int, QByteArray> roleNames() const;
// Destructor
~Node();
QString getNodeID() const;
Q_INVOKABLE bool setNodeID(const QString &nodeID);
QString getIconFilePath() const;
Q_INVOKABLE bool setIconFilePath(const QString &iconFilePath);
void hide();
void show();
void connectTo(Node* p_toNode);
void disconnectFrom(Node* p_toNode);
void removeNode();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
signals:
void nodeIDchanged(QString data);
void iconFilePathChanged(QString data);
void dataChanged();
protected:
void addParent(Node* p_parentNode);
const QList<Node*>& getParents() const;
QList<Node*>& accessParents();
void addChild(Node* p_childNode);
const QList<Node*>& getChildren() const;
QList<Node*>& accessChildren();
private:
QString _nodeID;
NodeType _nodeType;
PlugInType _plugInType;
QString _iconFilePath;
QList<Node*> _parents;
QList<Node*> _children;
};
#endif // NODE_H
To copy to clipboard, switch view to plain text mode
Bookmarks