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

Robert Voinea rvoinea at gmail.com
Sat Nov 10 23:35:52 CET 2012


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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20121111/d03b136e/attachment.html>


More information about the Interest mailing list