[Interest] Why is this not working? Assign direct to QML (through setContextProperty) works, as function doesn't.

Mark markg85 at gmail.com
Sun Nov 11 20:11:43 CET 2012


On Sat, Nov 10, 2012 at 11:35 PM, Robert Voinea <rvoinea at gmail.com> wrote:
>
>
> Hi
>
>
>
> On Saturday 10 November 2012 20:05:13 Mark wrote:
>
>> On Sat, Nov 10, 2012 at 8:03 PM, Robert Voinea <rvoinea at gmail.com> wrote:
>
>> > I am not saying to instantiate it as QObject, but expose it to qml as
>
>> > qobject.
>
>> >
>
>> > I'm sorry that my replies are so short... but i'm writing from my phone.
>
>> >
>
>> > As soon as I get in front of my pc I'll send a more detailed
>
>> > example/explanation...
>
>> >
>
>> > On Nov 10, 2012 8:54 PM, "Mark" <markg85 at gmail.com> wrote:
>
>> >> On Sat, Nov 10, 2012 at 7:47 PM, Robert Voinea <rvoinea at gmail.com>
>> >> wrote:
>
>> >> > This is working for me:
>
>> >> >
>
>> >> > instead of
>
>> >> > Q_INVOKABLE PathModel* pathModel();
>
>> >> > Try using
>
>> >> > Q_INVOKABLE QObject* pathModel();
>
>> >> >
>
>> >> > I believe there was a bug with custom types exported to qml...
>> >> > QVariant
>
>> >> > related...
>
>> >>
>
>> >> > On Nov 9, 2012 8:37 PM, "Mark" <markg85 at gmail.com> wrote:
>
>> >> Hi Robert,
>
>> >>
>
>> >> Could you explain a bit more? I'm actually using QStringListModel
>
>> >> function inside my Util class. These obviously don't work anymore if i
>
>> >> instantiate it as a QObject..
>
>> >>
>
>> >> Cheers,
>
>> >> Mark
>
>>
>
>> That would be very nice :)
>
>
>
> First of all... the bug I was talking about is this
>
> https://bugreports.qt-project.org/browse/QTBUG-13047
>
> But it seems to be fixed in Qt 4.7.1
>
>
>
> Well... I'd use something like this:
>
>
>
> pathmodel.h
>
> class PathModel : public QStringListModel
>
> {
>
> public:
>
> explicit PathModel(QObject *parent = 0);
>
> };
>
>
>
> util.h
>
>
>
> class Util : public QObject
>
> {
>
> Q_OBJECT
>
> Q_PROPERTY(QObject* pathModel READ pathModel CONSTANT)
>
> public:
>
> ...
>
> PathModel* pathModel() { return &m_pathModel; };
>
> Q_INVOKABLE QString testString() { return "some_test_text"; }
>
> private:
>
> PathModel m_pathModel;
>
> };
>
>
>
> with the downside of having to include the header for pathModel.
>
>
>
> As far as I know every variable, item, etc in QML is a QVariant.
>
>
>
> For QObject-derived classes their properties are exposed to QML and used
> through the QObject interface.
>
>
>
>
>
> --
>
> Robert Voinea
>
> IT Specialist
>
> +4 0740 467 262
>
>
>
> Don't take life too seriously. You'll never get out of it alive.
>
> (Elbert Hubbard)

Oke, i wasn't expecting that. I added the Q_PROPERTY(QObject*
pathModel READ pathModel CONSTANT) stuff to my code (and removed the
Q_INVOKABLE from my pathModel method), changed the code to use the new
property and amazingly it worked!

Why isn't this clearly documented _anywhere_? I certainly can use
google and know the Qt docs quite well, but i haven't found anything
that would indicate me to do it this way.

Another question, how can i get the same stuff working as a function
call? Now it works as a Q_PROPERTY which is fine for my case, but i'm
just curious about the same for a function call.

Cheers,
Mark



More information about the Interest mailing list