How to use a C class declared in a namespace with Q PROPERTY and QML: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(Remove non-functioning "toc" command)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''English''' [[How to use a C class declared in a namespace with Q PROPERTY and QML Bulgarian|Български]]
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}


=How to use a C++ class declared in a namespace with Q_PROPERTY and <span class="caps">QML</span>=
[[Category:HowTo]]
[[Category:Developing_with_Qt::General]]
[[Category:Tutorial]]


Using a C++ class declared in a namespace with <span class="caps">QML</span> works out of the box, in general. All you need to do is to ensure that you register the class for usage in <span class="caps">QML</span> with [http://doc.qt.nokia.com/latest/qdeclarativeengine.html#qmlRegisterType qmlRegisterType()] ''[doc.qt.nokia.com]''. When you need to reference the namespaced class in a [http://doc.qt.nokia.com/latest/qobject.html#Q_PROPERTY Q_PROPERTY] ''[doc.qt.nokia.com]'' however, then there are some concerns that need to be taken for this to work with <span class="caps">QML</span>. The example below illustrate the steps that need to be taken.
'''English''' [[:How_to_use_a_C_class_declared_in_a_namespace_with_Q_PROPERTY_and_QML_Bulgarian|Български]]


==A Simple Example==
 
= How to use a C++ class declared in a namespace with Q_PROPERTY and QML =
 
Using a C++ class declared in a namespace with QML works out of the box, in general. All you need to do is to ensure that you register the class for usage in QML with [http://doc.qt.nokia.com/latest/qdeclarativeengine.html#qmlRegisterType qmlRegisterType()]. When you need to reference the namespaced class in a [http://doc.qt.nokia.com/latest/qobject.html#Q_PROPERTY Q_PROPERTY] however, then there are some concerns that need to be taken for this to work with QML. The example below illustrate the steps that need to be taken.
 
== A Simple Example ==


The example below consists of 2 classes declared in a namespace:
The example below consists of 2 classes declared in a namespace:


The Person class is referenced in the Q_PROPERTY declaration of the BirthdayParty class. In order for the <span class="caps">QML</span> code using the classes to work as expected, then the namespace name has to be included when referring to the Person class in Q_PROPERTY declarations and in associated read and write function prototypes and also in [http://doc.qt.nokia.com/latest/qobject.html#Q_INVOKABLE Q_INVOKABLE] ''[doc.qt.nokia.com]'' function prototypes. In general, C++ does not have this kind of limitation; it is enough that the class declarations are inside the namespace and there is no need to use namespace names. There is a report in [https://bugreports.qt.nokia.com/browse/QTBUG-15459 Jira] ''[bugreports.qt.nokia.com]'' for improving the documentation on this.
<code>MyNamespace::Person
PartyNamespace::BirthdayParty </code>


===C++ implementation===
The Person class is referenced in the Q_PROPERTY declaration of the BirthdayParty class. In order for the QML code using the classes to work as expected, then the namespace name has to be included when referring to the Person class in Q_PROPERTY declarations and in associated read and write function prototypes and also in [http://doc.qt.nokia.com/latest/qobject.html#Q_INVOKABLE Q_INVOKABLE] function prototypes. In general, C++ does not have this kind of limitation; it is enough that the class declarations are inside the namespace and there is no need to use namespace names. There is a report in [https://bugreports.qt.nokia.com/browse/QTBUG-15459 Jira] for improving the documentation on this.
 
=== C++ implementation ===


In the C++ code below we ensure that the namespace MyNamespace is listed in front of the class name Person in the read and write functions for the BirthdayParty class below:
In the C++ code below we ensure that the namespace MyNamespace is listed in front of the class name Person in the read and write functions for the BirthdayParty class below:


main.cpp<br />
main.cpp
<code>
#include <QtGui>
#include <QtDeclarative>
 
namespace MyNamespace {
class Person : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
public:
Person()
{}
 
QString name() const
{
return m_name;
}
void setName(const QString & n)
{
m_name = n;
}
 
private:
QString m_name;
};
}
 
namespace PartyNamespace {
class BirthdayParty : public QObject
{
Q_OBJECT
Q_PROPERTY(MyNamespace::Person *host READ host WRITE setHost)
Q_PROPERTY(QDeclarativeListProperty<MyNamespace::Person> guests READ guests)
public:
BirthdayParty()
{}
 
MyNamespace::Person *host() const
{
return m_host;
}
void setHost(MyNamespace::Person *c)
{
m_host = c;
}
 
QDeclarativeListProperty<MyNamespace::Person> guests()
{
return QDeclarativeListProperty<MyNamespace::Person>(this, m_guests);
}
 
private:
MyNamespace::Person '''m_host;
QList<MyNamespace::Person'''> m_guests;
};
}
 
#include "main.moc"
 
using namespace MyNamespace;
using namespace PartyNamespace;
 
int main(int argc, char** argv)
{
QApplication app(argc, argv);
 
qmlRegisterType<Person>("People", 1, 0, "Person");
qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");
 
MyNamespace::Person myObj;
PartyNamespace::BirthdayParty myParty;


If the namespace is removed from these functions, the <span class="caps">QML</span> code using the classes will not work as expected.
QDeclarativeView view;
view.setSource(QUrl::fromLocalFile("main.qml"));
view.resize(200,100);
view.show();
return app.exec();
}


===<span class="caps">QML</span> implementation===
</code>


The example code below shows how the namespaced classes can be used in <span class="caps">QML</span>
If the namespace is removed from these functions, the QML code using the classes will not work as expected.
 
=== QML implementation ===
 
The example code below shows how the namespaced classes can be used in QML


main.qml
main.qml


===Categories:===
<code>
import People 1.0
import QtQuick 1.0
 
Text {
text: "Party people: " ''myPerson.name''" " + person1.name + " " + person2.name
BirthdayParty {
id: myParty;
host: Person {
id: myPerson
name: "Sigurd"
 
}
guests: [
Person { id: person1; name: "Jakob" },
Person { id: person2; name: "Dani" }
]
}
}


* [[:Category:Developing with Qt|Developing_with_Qt]]
</code>
** [[:Category:Developing with Qt::General|General]]
* [[:Category:HowTo|HowTo]]
* [[:Category:Tutorial|Tutorial]]

Latest revision as of 12:23, 17 April 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

English Български


How to use a C++ class declared in a namespace with Q_PROPERTY and QML

Using a C++ class declared in a namespace with QML works out of the box, in general. All you need to do is to ensure that you register the class for usage in QML with qmlRegisterType(). When you need to reference the namespaced class in a Q_PROPERTY however, then there are some concerns that need to be taken for this to work with QML. The example below illustrate the steps that need to be taken.

A Simple Example

The example below consists of 2 classes declared in a namespace:

MyNamespace::Person
PartyNamespace::BirthdayParty

The Person class is referenced in the Q_PROPERTY declaration of the BirthdayParty class. In order for the QML code using the classes to work as expected, then the namespace name has to be included when referring to the Person class in Q_PROPERTY declarations and in associated read and write function prototypes and also in Q_INVOKABLE function prototypes. In general, C++ does not have this kind of limitation; it is enough that the class declarations are inside the namespace and there is no need to use namespace names. There is a report in Jira for improving the documentation on this.

C++ implementation

In the C++ code below we ensure that the namespace MyNamespace is listed in front of the class name Person in the read and write functions for the BirthdayParty class below:

main.cpp

#include <QtGui>
#include <QtDeclarative>

namespace MyNamespace {
 class Person : public QObject
 {
 Q_OBJECT
 Q_PROPERTY(QString name READ name WRITE setName)
 public:
 Person()
 {}

QString name() const
 {
 return m_name;
 }
 void setName(const QString & n)
 {
 m_name = n;
 }

private:
 QString m_name;
 };
}

namespace PartyNamespace {
 class BirthdayParty : public QObject
 {
 Q_OBJECT
 Q_PROPERTY(MyNamespace::Person *host READ host WRITE setHost)
 Q_PROPERTY(QDeclarativeListProperty<MyNamespace::Person> guests READ guests)
 public:
 BirthdayParty()
 {}

MyNamespace::Person *host() const
 {
 return m_host;
 }
 void setHost(MyNamespace::Person *c)
 {
 m_host = c;
 }

QDeclarativeListProperty<MyNamespace::Person> guests()
 {
 return QDeclarativeListProperty<MyNamespace::Person>(this, m_guests);
 }

private:
 MyNamespace::Person '''m_host;
 QList<MyNamespace::Person'''> m_guests;
 };
}

#include "main.moc"

using namespace MyNamespace;
using namespace PartyNamespace;

int main(int argc, char** argv)
{
 QApplication app(argc, argv);

qmlRegisterType<Person>("People", 1, 0, "Person");
 qmlRegisterType<BirthdayParty>("People", 1,0, "BirthdayParty");

MyNamespace::Person myObj;
 PartyNamespace::BirthdayParty myParty;

QDeclarativeView view;
 view.setSource(QUrl::fromLocalFile("main.qml"));
 view.resize(200,100);
 view.show();
 return app.exec();
}

If the namespace is removed from these functions, the QML code using the classes will not work as expected.

QML implementation

The example code below shows how the namespaced classes can be used in QML

main.qml

import People 1.0
import QtQuick 1.0

Text {
 text: "Party people: " ''myPerson.name''" " + person1.name + " " + person2.name
 BirthdayParty {
 id: myParty;
 host: Person {
 id: myPerson
 name: "Sigurd"

}
 guests: [
 Person { id: person1; name: "Jakob" },
 Person { id: person2; name: "Dani" }
 ]
 }
}