Results 1 to 4 of 4

Thread: Accessing model's QQmlListProperty property in QML

  1. #1
    Join Date
    Dec 2014
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Accessing model's QQmlListProperty property in QML

    Hi!

    My situation:

    Trying to code a calendar with QML, so I am using Repeater { ... } that will display days in selected month. As a model for Repeater I'm using a list with instances of class, that represent data for every day:

    Qt Code:
    1. context->setContextProperty("_days_list", QVariant::fromValue(daysList));
    To copy to clipboard, switch view to plain text mode 

    For simplicity in class that represents data for days I have properties:

    Qt Code:
    1. Q_PROPERTY( int dayNum READ getDayNum WRITE setDayNum NOTIFY dayNumChanged)
    2. Q_PROPERTY( QQmlListProperty<CalEvent> getDayEvents READ getDayEvents CONSTANT)
    To copy to clipboard, switch view to plain text mode 

    I have no problem displaying "dayNum"

    Qt Code:
    1. Repeater {
    2. model: _days_list
    3. Rectangle {
    4. id: dayRectangle
    5. width: 25
    6. height: 25
    7. Text {
    8. id: daysText
    9. text: model.modelData.dayNum
    10. anchors.centerIn: parent
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    But there is my problem: how can I access QQmlListProperty<CalEvent> with events for each day? For example after double click on Rectangle that represents one tile in calendar's grid I want to open a Window, that will show names of all day's events.

    If i do this:
    Qt Code:
    1. onDoubleClicked: {
    2. var component = Qt.createComponent("eventDialog.qml");
    3. eventWin = component.createObject(mainWindow);
    4. console.log(model.modelData.getDayEvents)
    5. eventWin.show();
    6. }
    To copy to clipboard, switch view to plain text mode 

    I get output into console: "qml: [object Object]", but don't know how to access e.g first event of the day.

    In advance: thanks for help.

    P.S: my source codes for calendar are on github: here

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing model's QQmlListProperty property in QML

    You could simply use QList<QObjec*> as the property type.

    Or even make the class a list model and access the events as model data.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2014
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing model's QQmlListProperty property in QML

    Thanks for quick reply, but I am afraid I don't quite understand.
    I have tried to use QList<QObject*> as a property but I've failed
    Could you show me a way how to do it? Or some tutorial/documentation

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing model's QQmlListProperty property in QML

    It should work just like any other property.

    Qt Code:
    1. class MyClass :public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(QList<QObject*> dayEents READ getDayEvents CONSTANT)
    5.  
    6. QList<QObject*> getDayEvents() const { return m_dayEvents; }
    7. //...
    8.  
    9.  
    10. QList<QObject*> m_dayEvents;
    11. };
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 24th November 2014, 08:28
  2. QQmlListProperty Problem
    By sedi in forum Qt Quick
    Replies: 1
    Last Post: 5th October 2014, 01:33
  3. Replies: 0
    Last Post: 31st January 2014, 15:50
  4. Accessing parent IndexModel in a tree Model
    By Guett_31 in forum Qt Programming
    Replies: 1
    Last Post: 30th April 2013, 07:31
  5. Replies: 1
    Last Post: 30th September 2011, 19:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.