diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/qml/qml/qqmlcomponent.cpp | 10 | ||||
| -rw-r--r-- | src/qml/qml/qqmlcomponent.h | 2 | ||||
| -rw-r--r-- | src/quickcontrolstestutils/CMakeLists.txt | 8 | ||||
| -rw-r--r-- | src/quickcontrolstestutils/controlstestutils.cpp | 14 | ||||
| -rw-r--r-- | src/quickcontrolstestutils/controlstestutils_p.h | 12 | ||||
| -rw-r--r-- | src/quickdialogs2/quickdialogs2quickimpl/qquickfolderbreadcrumbbar.cpp | 16 | ||||
| -rw-r--r-- | src/quicktemplates2/qquickmenu.cpp | 8 | ||||
| -rw-r--r-- | src/quicktemplates2/qquickmenubar.cpp | 9 | ||||
| -rw-r--r-- | src/quicktemplates2/qquickpopup.cpp | 8 | ||||
| -rw-r--r-- | src/quicktemplates2/qquicksplitview.cpp | 8 | ||||
| -rw-r--r-- | src/quicktemplates2/qquickstackelement.cpp | 10 | ||||
| -rw-r--r-- | src/quicktemplates2/qquickstackelement_p_p.h | 1 | ||||
| -rw-r--r-- | src/quicktemplates2/qquickswipedelegate.cpp | 8 |
13 files changed, 75 insertions, 39 deletions
diff --git a/src/qml/qml/qqmlcomponent.cpp b/src/qml/qml/qqmlcomponent.cpp index bce95da36a..beb3f0efe0 100644 --- a/src/qml/qml/qqmlcomponent.cpp +++ b/src/qml/qml/qqmlcomponent.cpp @@ -490,6 +490,16 @@ bool QQmlComponent::isLoading() const } /*! + Returns true if the component was created in a QML files that specifies + \c{pragma ComponentBehavior: Bound}, otherwise returns false. + */ +bool QQmlComponent::isBound() const +{ + Q_D(const QQmlComponent); + return d->isBound(); +} + +/*! \qmlproperty real Component::progress The progress of loading the component, from 0.0 (nothing loaded) to 1.0 (finished). diff --git a/src/qml/qml/qqmlcomponent.h b/src/qml/qml/qqmlcomponent.h index ccfece00f9..ca95d756a5 100644 --- a/src/qml/qml/qqmlcomponent.h +++ b/src/qml/qml/qqmlcomponent.h @@ -63,6 +63,8 @@ public: bool isError() const; bool isLoading() const; + bool isBound() const; + QList<QQmlError> errors() const; Q_INVOKABLE QString errorString() const; diff --git a/src/quickcontrolstestutils/CMakeLists.txt b/src/quickcontrolstestutils/CMakeLists.txt index 45b8569cfb..ac3c92a7f9 100644 --- a/src/quickcontrolstestutils/CMakeLists.txt +++ b/src/quickcontrolstestutils/CMakeLists.txt @@ -26,3 +26,11 @@ qt_internal_add_module(QuickControlsTestUtilsPrivate Qt::QuickTemplates2Private Qt::QuickTestUtilsPrivate ) + +# This is used by both C++ and QML tests, so we need it to be a library and a QML plugin, +# hence qt_internal_add_qml_module. We use it in addition to qt_internal_add_module, +# because otherwise syncqt complains that there is no "QtQuickControlsTestUtilsPrivate" module. +qt_internal_add_qml_module(QuickControlsTestUtilsPrivate + URI "Qt.test.controls" + VERSION "${PROJECT_VERSION}" +) diff --git a/src/quickcontrolstestutils/controlstestutils.cpp b/src/quickcontrolstestutils/controlstestutils.cpp index df65c3234b..de7d38db32 100644 --- a/src/quickcontrolstestutils/controlstestutils.cpp +++ b/src/quickcontrolstestutils/controlstestutils.cpp @@ -4,6 +4,7 @@ #include "controlstestutils_p.h" #include <QtTest/qsignalspy.h> +#include <QtQml/qqmlcomponent.h> #include <QtQuickControls2/qquickstyle.h> #include <QtQuickTemplates2/private/qquickabstractbutton_p.h> #include <QtQuickTemplates2/private/qquickapplicationwindow_p.h> @@ -155,3 +156,16 @@ bool QQuickControlsTestUtils::doubleClickButton(QQuickAbstractButton *button) return true; } + +/*! + Allows creating QQmlComponents in C++, which is useful for tests that need + to check if items created from the component have the correct QML context. +*/ +Q_INVOKABLE QQmlComponent *QQuickControlsTestUtils::ComponentCreator::createComponent(const QByteArray &data) +{ + std::unique_ptr<QQmlComponent> component(new QQmlComponent(qmlEngine(this))); + component->setData(data, QUrl()); + if (component->isError()) + qmlWarning(this) << "Failed to create component from the following data:\n" << data; + return component.release(); +} diff --git a/src/quickcontrolstestutils/controlstestutils_p.h b/src/quickcontrolstestutils/controlstestutils_p.h index 4d79d4af52..487d3b79fe 100644 --- a/src/quickcontrolstestutils/controlstestutils_p.h +++ b/src/quickcontrolstestutils/controlstestutils_p.h @@ -19,6 +19,7 @@ QT_BEGIN_NAMESPACE +class QQmlComponent; class QQmlEngine; class QQuickApplicationWindow; class QQuickAbstractButton; @@ -53,6 +54,17 @@ namespace QQuickControlsTestUtils [[nodiscard]] bool verifyButtonClickable(QQuickAbstractButton *button); [[nodiscard]] bool clickButton(QQuickAbstractButton *button); [[nodiscard]] bool doubleClickButton(QQuickAbstractButton *button); + + class ComponentCreator : public QObject + { + Q_OBJECT + QML_ELEMENT + QML_SINGLETON + Q_MOC_INCLUDE(<QtQml/qqmlcomponent.h>) + + public: + Q_INVOKABLE QQmlComponent *createComponent(const QByteArray &data); + }; } QT_END_NAMESPACE diff --git a/src/quickdialogs2/quickdialogs2quickimpl/qquickfolderbreadcrumbbar.cpp b/src/quickdialogs2/quickdialogs2quickimpl/qquickfolderbreadcrumbbar.cpp index 8fb117b167..a78833f470 100644 --- a/src/quickdialogs2/quickdialogs2quickimpl/qquickfolderbreadcrumbbar.cpp +++ b/src/quickdialogs2/quickdialogs2quickimpl/qquickfolderbreadcrumbbar.cpp @@ -35,13 +35,19 @@ QQuickItem *QQuickFolderBreadcrumbBarPrivate::createDelegateItem(QQmlComponent * Q_Q(QQuickFolderBreadcrumbBar); // If we don't use the correct context, it won't be possible to refer to // the control's id from within the delegates. - QQmlContext *creationContext = component->creationContext(); + QQmlContext *context = component->creationContext(); // The component might not have been created in QML, in which case // the creation context will be null and we have to create it ourselves. - if (!creationContext) - creationContext = qmlContext(q); - QQmlContext *context = new QQmlContext(creationContext, q); - context->setContextObject(q); + if (!context) + context = qmlContext(q); + + // If we have initial properties we assume that all necessary information is passed via + // initial properties. + if (!component->isBound() && initialProperties.isEmpty()) { + context = new QQmlContext(context, q); + context->setContextObject(q); + } + QQuickItem *item = qobject_cast<QQuickItem*>(component->createWithInitialProperties(initialProperties, context)); if (item) QQml_setParent_noEvent(item, q); diff --git a/src/quicktemplates2/qquickmenu.cpp b/src/quicktemplates2/qquickmenu.cpp index 20280824c0..f5c59ed4e0 100644 --- a/src/quicktemplates2/qquickmenu.cpp +++ b/src/quicktemplates2/qquickmenu.cpp @@ -254,11 +254,9 @@ QQuickItem *QQuickMenuPrivate::beginCreateItem() if (!delegate) return nullptr; - QQmlContext *creationContext = delegate->creationContext(); - if (!creationContext) - creationContext = qmlContext(q); - QQmlContext *context = new QQmlContext(creationContext, q); - context->setContextObject(q); + QQmlContext *context = delegate->creationContext(); + if (!context) + context = qmlContext(q); QObject *object = delegate->beginCreate(context); QQuickItem *item = qobject_cast<QQuickItem *>(object); diff --git a/src/quicktemplates2/qquickmenubar.cpp b/src/quicktemplates2/qquickmenubar.cpp index f638320286..3f7fd853d4 100644 --- a/src/quicktemplates2/qquickmenubar.cpp +++ b/src/quicktemplates2/qquickmenubar.cpp @@ -49,17 +49,14 @@ QQuickItem *QQuickMenuBarPrivate::beginCreateItem(QQuickMenu *menu) if (!delegate) return nullptr; - QQmlContext *creationContext = delegate->creationContext(); - if (!creationContext) - creationContext = qmlContext(q); - QQmlContext *context = new QQmlContext(creationContext, q); - context->setContextObject(q); + QQmlContext *context = delegate->creationContext(); + if (!context) + context = qmlContext(q); QObject *object = delegate->beginCreate(context); QQuickItem *item = qobject_cast<QQuickItem *>(object); if (!item) { delete object; - delete context; return nullptr; } diff --git a/src/quicktemplates2/qquickpopup.cpp b/src/quicktemplates2/qquickpopup.cpp index 7cf77ca75f..95b858563d 100644 --- a/src/quicktemplates2/qquickpopup.cpp +++ b/src/quicktemplates2/qquickpopup.cpp @@ -715,11 +715,9 @@ static QQuickItem *createDimmer(QQmlComponent *component, QQuickPopup *popup, QQ { QQuickItem *item = nullptr; if (component) { - QQmlContext *creationContext = component->creationContext(); - if (!creationContext) - creationContext = qmlContext(popup); - QQmlContext *context = new QQmlContext(creationContext, popup); - context->setContextObject(popup); + QQmlContext *context = component->creationContext(); + if (!context) + context = qmlContext(popup); item = qobject_cast<QQuickItem*>(component->beginCreate(context)); } diff --git a/src/quicktemplates2/qquicksplitview.cpp b/src/quicktemplates2/qquicksplitview.cpp index c0f894ffa5..7f6a86f116 100644 --- a/src/quicktemplates2/qquicksplitview.cpp +++ b/src/quicktemplates2/qquicksplitview.cpp @@ -700,13 +700,11 @@ void QQuickSplitViewPrivate::createHandleItem(int index) // If we don't use the correct context, it won't be possible to refer to // the control's id from within the delegate. - QQmlContext *creationContext = m_handle->creationContext(); + QQmlContext *context = m_handle->creationContext(); // The component might not have been created in QML, in which case // the creation context will be null and we have to create it ourselves. - if (!creationContext) - creationContext = qmlContext(q); - QQmlContext *context = new QQmlContext(creationContext, q); - context->setContextObject(q); + if (!context) + context = qmlContext(q); QQuickItem *handleItem = qobject_cast<QQuickItem*>(m_handle->beginCreate(context)); if (handleItem) { handleItem->setParent(q); diff --git a/src/quicktemplates2/qquickstackelement.cpp b/src/quicktemplates2/qquickstackelement.cpp index 09790d4f93..e165a780bb 100644 --- a/src/quicktemplates2/qquickstackelement.cpp +++ b/src/quicktemplates2/qquickstackelement.cpp @@ -79,8 +79,6 @@ QQuickStackElement::~QQuickStackElement() if (attached) emit attached->removed(); - - delete context; } QQuickStackElement *QQuickStackElement::fromString(const QString &str, QQuickStackView *view, QString *error) @@ -134,11 +132,9 @@ bool QQuickStackElement::load(QQuickStackView *parent) return true; } - QQmlContext *creationContext = component->creationContext(); - if (!creationContext) - creationContext = qmlContext(parent); - context = new QQmlContext(creationContext, parent); - context->setContextObject(parent); + QQmlContext *context = component->creationContext(); + if (!context) + context = qmlContext(parent); QQuickStackIncubator incubator(this); component->create(incubator, context); diff --git a/src/quicktemplates2/qquickstackelement_p_p.h b/src/quicktemplates2/qquickstackelement_p_p.h index 2e38e42c47..f591e8fd5d 100644 --- a/src/quicktemplates2/qquickstackelement_p_p.h +++ b/src/quicktemplates2/qquickstackelement_p_p.h @@ -61,7 +61,6 @@ public: bool ownComponent = false; bool widthValid = false; bool heightValid = false; - QQmlContext *context = nullptr; QQmlComponent *component = nullptr; QQuickStackView *view = nullptr; QPointer<QQuickItem> originalParent; diff --git a/src/quicktemplates2/qquickswipedelegate.cpp b/src/quicktemplates2/qquickswipedelegate.cpp index f3c57fe8fa..049856597c 100644 --- a/src/quicktemplates2/qquickswipedelegate.cpp +++ b/src/quicktemplates2/qquickswipedelegate.cpp @@ -185,13 +185,11 @@ QQuickItem *QQuickSwipePrivate::createDelegateItem(QQmlComponent *component) { // If we don't use the correct context, it won't be possible to refer to // the control's id from within the delegates. - QQmlContext *creationContext = component->creationContext(); + QQmlContext *context = component->creationContext(); // The component might not have been created in QML, in which case // the creation context will be null and we have to create it ourselves. - if (!creationContext) - creationContext = qmlContext(control); - QQmlContext *context = new QQmlContext(creationContext, control); - context->setContextObject(control); + if (!context) + context = qmlContext(control); QQuickItem *item = qobject_cast<QQuickItem*>(component->beginCreate(context)); if (item) { item->setParentItem(control); |
