aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-04-07 11:50:39 +0200
committerUlf Hermann <ulf.hermann@qt.io>2020-04-28 11:46:03 +0200
commitc2de5643cd4f1b8d8b10e2bb62fdf95f12fdd9e3 (patch)
tree55cfa6610c91315d5ae6cbbf2c961fb2fbfcabd8 /src/qml
parent1029b2b9f3d0ff88c0900fbfec2fac873aa6bcd4 (diff)
Register value types declaratively
For now this has the effect of adding them to the .qmltypes files. In the future, the registration shall actually add additional value types you can declare as properties in QML. Change-Id: Ifee5a8ec054f35cc7bd07eb992a136730be68da7 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml')
-rw-r--r--src/qml/qml/qqml.h4
-rw-r--r--src/qml/qml/qqmlmetatype.cpp22
-rw-r--r--src/qml/qml/qqmlprivate.h8
-rw-r--r--src/qml/qml/qqmlvaluetype_p.h28
-rw-r--r--src/qml/types/qqmlmodelindexvaluetype_p.h10
5 files changed, 61 insertions, 11 deletions
diff --git a/src/qml/qml/qqml.h b/src/qml/qml/qqml.h
index 252e4a5baf..4957c12036 100644
--- a/src/qml/qml/qqml.h
+++ b/src/qml/qml/qqml.h
@@ -77,6 +77,10 @@
#define QML_NAMED_ELEMENT(NAME) \
Q_CLASSINFO("QML.Element", #NAME)
+#define QML_VALUE_TYPE(NAME) \
+ Q_CLASSINFO("QML.Element", #NAME) \
+ QML_UNCREATABLE("Value types cannot be created.")
+
#define QML_UNCREATABLE(REASON) \
Q_CLASSINFO("QML.Creatable", "false") \
Q_CLASSINFO("QML.UncreatableReason", REASON)
diff --git a/src/qml/qml/qqmlmetatype.cpp b/src/qml/qml/qqmlmetatype.cpp
index 51931ccb6c..4587ac3cc8 100644
--- a/src/qml/qml/qqmlmetatype.cpp
+++ b/src/qml/qml/qqmlmetatype.cpp
@@ -366,10 +366,12 @@ QString registrationTypeString(QQmlType::RegistrationType typeType)
// NOTE: caller must hold a QMutexLocker on "data"
bool checkRegistration(QQmlType::RegistrationType typeType, QQmlMetaTypeData *data,
- const char *uri, const QString &typeName, QTypeRevision version)
+ const char *uri, const QString &typeName, QTypeRevision version,
+ QMetaType::TypeFlags flags)
{
if (!typeName.isEmpty()) {
- if (typeName.at(0).isLower()) {
+ if (typeName.at(0).isLower()
+ && !(flags & (QMetaType::PointerToGadget | QMetaType::IsGadget))) {
QString failure(QCoreApplication::translate("qmlRegisterType", "Invalid QML %1 name \"%2\"; type names must begin with an uppercase letter"));
data->recordTypeRegFailure(failure.arg(registrationTypeString(typeType)).arg(typeName));
return false;
@@ -453,8 +455,10 @@ QQmlType QQmlMetaType::registerType(const QQmlPrivate::RegisterType &type)
QQmlMetaTypeDataPtr data;
QString elementName = QString::fromUtf8(type.elementName);
- if (!checkRegistration(QQmlType::CppType, data, type.uri, elementName, type.version))
+ if (!checkRegistration(QQmlType::CppType, data, type.uri, elementName, type.version,
+ QMetaType(type.typeId).flags())) {
return QQmlType();
+ }
QQmlTypePrivate *priv = createQQmlType(data, elementName, type);
@@ -473,8 +477,10 @@ QQmlType QQmlMetaType::registerSingletonType(const QQmlPrivate::RegisterSingleto
QQmlMetaTypeDataPtr data;
QString typeName = QString::fromUtf8(type.typeName);
- if (!checkRegistration(QQmlType::SingletonType, data, type.uri, typeName, type.version))
+ if (!checkRegistration(QQmlType::SingletonType, data, type.uri, typeName, type.version,
+ QMetaType(type.typeId).flags())) {
return QQmlType();
+ }
QQmlTypePrivate *priv = createQQmlType(data, typeName, type);
@@ -496,7 +502,7 @@ QQmlType QQmlMetaType::registerCompositeSingletonType(const QQmlPrivate::Registe
if (*(type.uri) == '\0')
fileImport = true;
if (!checkRegistration(QQmlType::CompositeSingletonType, data, fileImport ? nullptr : type.uri,
- typeName, type.version)) {
+ typeName, type.version, {})) {
return QQmlType();
}
@@ -521,8 +527,10 @@ QQmlType QQmlMetaType::registerCompositeType(const QQmlPrivate::RegisterComposit
bool fileImport = false;
if (*(type.uri) == '\0')
fileImport = true;
- if (!checkRegistration(QQmlType::CompositeType, data, fileImport?nullptr:type.uri, typeName, type.version))
+ if (!checkRegistration(QQmlType::CompositeType, data, fileImport?nullptr:type.uri, typeName,
+ type.version, {})) {
return QQmlType();
+ }
QQmlTypePrivate *priv = createQQmlType(data, typeName, type);
addTypeToData(priv, data);
@@ -814,7 +822,7 @@ QQmlType QQmlMetaType::typeForUrl(const QString &urlString,
const QQmlType::RegistrationType registrationType = isCompositeSingleton
? QQmlType::CompositeSingletonType
: QQmlType::CompositeType;
- if (checkRegistration(registrationType, data, nullptr, typeName, version)) {
+ if (checkRegistration(registrationType, data, nullptr, typeName, version, {})) {
auto *priv = new QQmlTypePrivate(registrationType);
priv->setName(QString(), typeName);
priv->version = version;
diff --git a/src/qml/qml/qqmlprivate.h b/src/qml/qml/qqmlprivate.h
index 79969caa56..c5c524c27a 100644
--- a/src/qml/qml/qqmlprivate.h
+++ b/src/qml/qml/qqmlprivate.h
@@ -534,9 +534,9 @@ namespace QQmlPrivate
if (qstrcmp(elementName, "anonymous") == 0)
return nullptr;
- if (!elementName || elementName[0] < 'A' || elementName[0] > 'Z') {
- qWarning() << "Missing or unusable QML.Element class info \"" << elementName << "\""
- << "for" << metaObject->className();
+ if (!elementName) {
+ qWarning().nospace() << "Missing QML.Element class info \"" << elementName << "\""
+ << " for " << metaObject->className();
}
return elementName;
@@ -561,7 +561,7 @@ namespace QQmlPrivate
};
template<class T>
- struct QmlResolved<T, QmlVoidT<typename T::QmlForeignType>>
+ struct QmlResolved<T, QmlVoidT<decltype(T::QmlForeignType::staticMetaObject)>>
{
using Type = typename T::QmlForeignType;
};
diff --git a/src/qml/qml/qqmlvaluetype_p.h b/src/qml/qml/qqmlvaluetype_p.h
index 0b38c746d6..056baf87d0 100644
--- a/src/qml/qml/qqmlvaluetype_p.h
+++ b/src/qml/qml/qqmlvaluetype_p.h
@@ -133,6 +133,10 @@ struct QQmlPointFValueType
Q_PROPERTY(qreal x READ x WRITE setX FINAL)
Q_PROPERTY(qreal y READ y WRITE setY FINAL)
Q_GADGET
+ QML_VALUE_TYPE(point)
+ QML_FOREIGN(QPointF)
+ QML_ADDED_IN_VERSION(2, 0)
+
public:
Q_INVOKABLE QString toString() const;
qreal x() const;
@@ -147,6 +151,10 @@ struct QQmlPointValueType
Q_PROPERTY(int x READ x WRITE setX FINAL)
Q_PROPERTY(int y READ y WRITE setY FINAL)
Q_GADGET
+ QML_ANONYMOUS
+ QML_FOREIGN(QPoint)
+ QML_ADDED_IN_VERSION(2, 0)
+
public:
int x() const;
int y() const;
@@ -160,6 +168,10 @@ struct QQmlSizeFValueType
Q_PROPERTY(qreal width READ width WRITE setWidth FINAL)
Q_PROPERTY(qreal height READ height WRITE setHeight FINAL)
Q_GADGET
+ QML_VALUE_TYPE(size)
+ QML_FOREIGN(QSizeF)
+ QML_ADDED_IN_VERSION(2, 0)
+
public:
Q_INVOKABLE QString toString() const;
qreal width() const;
@@ -174,6 +186,10 @@ struct QQmlSizeValueType
Q_PROPERTY(int width READ width WRITE setWidth FINAL)
Q_PROPERTY(int height READ height WRITE setHeight FINAL)
Q_GADGET
+ QML_ANONYMOUS
+ QML_FOREIGN(QSize)
+ QML_ADDED_IN_VERSION(2, 0)
+
public:
int width() const;
int height() const;
@@ -193,6 +209,10 @@ struct QQmlRectFValueType
Q_PROPERTY(qreal top READ top DESIGNABLE false FINAL)
Q_PROPERTY(qreal bottom READ bottom DESIGNABLE false FINAL)
Q_GADGET
+ QML_VALUE_TYPE(rect)
+ QML_FOREIGN(QRectF)
+ QML_ADDED_IN_VERSION(2, 0)
+
public:
Q_INVOKABLE QString toString() const;
qreal x() const;
@@ -223,6 +243,10 @@ struct QQmlRectValueType
Q_PROPERTY(int top READ top DESIGNABLE false FINAL)
Q_PROPERTY(int bottom READ bottom DESIGNABLE false FINAL)
Q_GADGET
+ QML_ANONYMOUS
+ QML_FOREIGN(QRect)
+ QML_ADDED_IN_VERSION(2, 0)
+
public:
int x() const;
int y() const;
@@ -302,6 +326,10 @@ struct QQmlPropertyValueType
Q_PROPERTY(QObject *object READ object CONSTANT FINAL)
Q_PROPERTY(QString name READ name CONSTANT FINAL)
Q_GADGET
+ QML_ANONYMOUS
+ QML_FOREIGN(QQmlProperty)
+ QML_ADDED_IN_VERSION(2, 15)
+
public:
QObject *object() const;
QString name() const;
diff --git a/src/qml/types/qqmlmodelindexvaluetype_p.h b/src/qml/types/qqmlmodelindexvaluetype_p.h
index f5b1699574..59c33bf5af 100644
--- a/src/qml/types/qqmlmodelindexvaluetype_p.h
+++ b/src/qml/types/qqmlmodelindexvaluetype_p.h
@@ -53,6 +53,7 @@
#include <QtCore/qabstractitemmodel.h>
#include <QtCore/qitemselectionmodel.h>
+#include <QtQml/qqml.h>
QT_BEGIN_NAMESPACE
@@ -67,6 +68,9 @@ struct QQmlModelIndexValueType
Q_PROPERTY(QAbstractItemModel *model READ model CONSTANT FINAL)
Q_PROPERTY(quint64 internalId READ internalId CONSTANT FINAL)
Q_GADGET
+ QML_ANONYMOUS
+ QML_FOREIGN(QModelIndex)
+ QML_ADDED_IN_VERSION(2, 0)
public:
Q_INVOKABLE QString toString() const
@@ -97,6 +101,9 @@ struct QQmlPersistentModelIndexValueType
Q_PROPERTY(QAbstractItemModel *model READ model FINAL)
Q_PROPERTY(quint64 internalId READ internalId FINAL)
Q_GADGET
+ QML_ANONYMOUS
+ QML_FOREIGN(QPersistentModelIndex)
+ QML_ADDED_IN_VERSION(2, 0)
public:
Q_INVOKABLE QString toString() const
@@ -130,6 +137,9 @@ struct QQmlItemSelectionRangeValueType
Q_PROPERTY(bool empty READ isEmpty FINAL)
Q_PROPERTY(QAbstractItemModel *model READ model FINAL)
Q_GADGET
+ QML_ANONYMOUS
+ QML_FOREIGN(QItemSelectionRange)
+ QML_ADDED_IN_VERSION(2, 0)
public:
Q_INVOKABLE QString toString() const;