[Interest] Enumerating context properties from QML

Alexander Ivash elderorb at gmail.com
Thu Dec 21 22:04:25 CET 2017


What I mean, is that the following code works in debug without assertions:

#include <QGuiApplication>

#include <QQmlApplicationEngine>

#include <QQmlContext>

#include "myobject.h"


int main(int argc, char *argv[])

{

#if defined(Q_OS_WIN)

    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

#endif


    QGuiApplication app(argc, argv);


    QQmlApplicationEngine engine;


    Q_ASSERT(engine.rootContext()->contextObject() == nullptr);


    MyObject object;

    engine.rootContext()->setContextProperty("myObject", &object);


    Q_ASSERT(engine.rootContext()->contextObject() == nullptr);


    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    if (engine.rootObjects().isEmpty())

        return -1;


    Q_ASSERT(engine.rootContext()->contextObject() == nullptr);


    return app.exec();

}



2017-12-20 19:37 GMT+00:00 Jérôme Godbout <jerome at bodycad.com>:

> If the context object is null, you probably have an error somewhere, your
> context should outlive your object, unless you are during the creation of
> the object step (after the QQmlComponent beginCreate() but before
> the completeCreate() call?). If so indeed the object is not completed yet.
> If this is not the case, I'm not sure I understand when you try to do this
> or how you have a null context. Sorry for the noise, might help someone to
> figure it out.
>
> [image: bodycad] <https://www.bodycad.com/>
> Jerome Godbout
> Software Developer
> 2035 rue du Haut-Bord, Québec, QC, Canada. G1N 4R7
> <https://maps.google.com/?q=2035+rue+du+Haut-Bord,+Qu%C3%A9bec,+QC,+Canada.+G1N+4R7&entry=gmail&source=g>
> T:  +1 418 527-1388 <(418)%20527-1388>
> E: jerome at bodycad.com
> www.bodycad.com
>
> The contents of this email message and any attachments are intended solely
> for the addressee(s) and may contain confidential and/or privileged
> information and may be legally protected from disclosure. If you are not
> the intended recipient of this message or their agent, or if this message
> has been addressed to you in error, please immediately alert the sender by
> reply email and then delete this message and any attachments. If you are
> not the intended recipient, you are hereby notified that any use,
> dissemination, copying, or storage of this message or its attachments is
> strictly prohibited.
>
> Le contenu de ce message et les pièces jointes sont destinés uniquement
> pour le(s) destinataire(s) et peuvent contenir des informations
> confidentielles et / ou privilégiées qui peuvent être protégées légalement
> contre toute divulgation. Si vous n'êtes pas le destinataire de ce message
> ou son agent, ou si ce message vous a été adressé par erreur, s’il vous
> plaît avertir immédiatement l'expéditeur par courriel de réponse, puis
> supprimer ce message et les pièces jointes. Si vous n'êtes pas le
> destinataire prévu, vous êtes par la présente informé que toute
> utilisation, diffusion, copie, ou stockage de ce message ou de ses pièces
> jointes est strictement interdit.
>
> On Wed, Dec 20, 2017 at 1:19 PM, Alexander Ivash <elderorb at gmail.com>
> wrote:
>
>> That's what I already tried but the issue is that context object is null
>> before and after setting context properties/loading QML.
>>
>> Anyway, thank you for reply!
>>
>> 2017-12-20 16:21 GMT+00:00 Jérôme Godbout <jerome at bodycad.com>:
>>
>>> I'm not sure it will work but one might try. You might recover the
>>> object context and maybe scan the meta object to find the properties.
>>>
>>> *auto context = QQmlEngine::contextForObject(myObjPtr);*
>>>
>>> Since there's no getter into the context directly. The only getter is
>>> *contextProperty(const QString& name)*
>>>
>>> Maybe the meta object of the context could hold some information, not
>>> sure it will help but can give some idea.
>>> *auto meta_obj = context->metaObject();*
>>>
>>> Iterate on properties to see if you can extract some information out of
>>> it
>>> *for(int i = 0; i < meta_obj->propertyCount(); ++i)*
>>> *{*
>>> *  auto meta_property = **meta_obj->**property(i);*
>>>
>>> *  // check if the property hold anything of interest here
>>> http://doc.qt.io/qt-5/qmetaproperty.html
>>> <http://doc.qt.io/qt-5/qmetaproperty.html>*
>>> *}*
>>>
>>> I'm really not sure if this could extract anything from Context value
>>> but might worth a small test. Check into the meta info, maybe something can
>>> salvage the information (we do this to inspect particular object to debug
>>> them but for Context, I never really tried.
>>>
>>> [image: bodycad] <https://www.bodycad.com/>
>>> Jerome Godbout
>>> Software Developer
>>> 2035 rue du Haut-Bord, Québec, QC, Canada. G1N 4R7
>>> <https://maps.google.com/?q=2035+rue+du+Haut-Bord,+Qu%C3%A9bec,+QC,+Canada.+G1N+4R7&entry=gmail&source=g>
>>> T:  +1 418 527-1388 <(418)%20527-1388>
>>> E: jerome at bodycad.com
>>> www.bodycad.com
>>>
>>> The contents of this email message and any attachments are intended
>>> solely for the addressee(s) and may contain confidential and/or privileged
>>> information and may be legally protected from disclosure. If you are not
>>> the intended recipient of this message or their agent, or if this message
>>> has been addressed to you in error, please immediately alert the sender by
>>> reply email and then delete this message and any attachments. If you are
>>> not the intended recipient, you are hereby notified that any use,
>>> dissemination, copying, or storage of this message or its attachments is
>>> strictly prohibited.
>>>
>>> Le contenu de ce message et les pièces jointes sont destinés uniquement
>>> pour le(s) destinataire(s) et peuvent contenir des informations
>>> confidentielles et / ou privilégiées qui peuvent être protégées légalement
>>> contre toute divulgation. Si vous n'êtes pas le destinataire de ce message
>>> ou son agent, ou si ce message vous a été adressé par erreur, s’il vous
>>> plaît avertir immédiatement l'expéditeur par courriel de réponse, puis
>>> supprimer ce message et les pièces jointes. Si vous n'êtes pas le
>>> destinataire prévu, vous êtes par la présente informé que toute
>>> utilisation, diffusion, copie, ou stockage de ce message ou de ses pièces
>>> jointes est strictement interdit.
>>>
>>> On Tue, Dec 19, 2017 at 7:08 PM, Alexander Ivash <elderorb at gmail.com>
>>> wrote:
>>>
>>>> Let's say I exposed 'property1' to QML from C++ using
>>>> setContextProperty. How can I enumerate all the context properties
>>>> exposed from QML side? I've tried enumerating properties of 'Qt'
>>>> object but obviously it didn't work.
>>>> _______________________________________________
>>>> Interest mailing list
>>>> Interest at qt-project.org
>>>> http://lists.qt-project.org/mailman/listinfo/interest
>>>>
>>>
>>>
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171221/affa46c4/attachment.html>


More information about the Interest mailing list