Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Is it possible to dynamically populate a ListView within a GridView ?

  1. #1
    Join Date
    Mar 2016
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question Is it possible to dynamically populate a ListView within a GridView ?

    In our application, we need to display a GridView wich contains some elements of custom type OverviewEntry and in these elements, we have a QList of element of custom type ChannelEntry.

    Some code is more usefull than a long description so, please found the related code (simplified, of course) :

    Qt Code:
    1. //Main.cpp
    2.  
    3. ...
    4. m_viewer = new QQuickView();
    5. ...
    6. QList<ChannelEntry *> channels;
    7. for (int i=0; i<5; i++) {
    8. for (int j=0; j<4; j++) {
    9. channels.append(new ChannelEntry("foo"));
    10. }
    11. m_overviewList.append(new OverviewEntry("bar", channels));
    12. }
    13. ...
    14. QQmlContext *ctxt = m_viewer->rootContext();
    15. ctxt->setContextProperty("overviewModel", QVariant::fromValue(m_overviewList));
    16. ...
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //OverviewEntry.h
    2.  
    3. class OverviewEntry : public QObject
    4. {
    5. Q_OBJECT
    6. Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    7. ...
    8. Q_PROPERTY(QList<ChannelEntry> channels READ channels WRITE setChannels NOTIFY channelsChanged)
    9. ...
    10. QString m_name;
    11. QList<ChannelEntry *> m_channels;
    12. };
    13.  
    14. Q_DECLARE_METATYPE(QList<ChannelEntry>)
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //ChannelEntry.h
    2.  
    3. class ChannelEntry : public QObject
    4. {
    5. Q_OBJECT
    6. Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    7. ...
    8. QString m_name;
    9. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //Main.qml
    2.  
    3. ...
    4. GridView {
    5. model: overviewModel
    6. delegate : OverviewEntry {}
    7. }
    8. ...
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //OverviewEntry.qml
    2.  
    3. ...
    4. Text{
    5. text: modelData.name
    6. }
    7. ...
    8. ListView {
    9. model: modelData.channels
    10. delegate : ChannelEntry {}
    11. }
    12. ...
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //ChannelEntry.qml
    2.  
    3. ...
    4. Text{
    5. text: modelData.name
    6. }
    7. ...
    To copy to clipboard, switch view to plain text mode 

    My GridView is well populated but my ListView remains empty... Even after 2-3 days of trying different approach...

    So what am I missing ? Anyone has a clue, please ?

  2. #2
    Join Date
    Jan 2006
    Location
    Earth (Terra)
    Posts
    87
    Thanks
    4
    Thanked 6 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Should this:

    Qt Code:
    1. Q_PROPERTY(QList<ChannelEntry> channels READ channels WRITE setChannels NOTIFY channelsChanged)
    To copy to clipboard, switch view to plain text mode 
    be

    Qt Code:
    1. Q_PROPERTY(QList<ChannelEntry *> channels READ channels WRITE setChannels NOTIFY channelsChanged)
    To copy to clipboard, switch view to plain text mode 

    ?

    [STRIKE]Also, I think all your properties need to point to the 'm_'<variableName>, where you've declared them as such[/STRIKE]
    (no see below & also 'Property System' topic in doc.)
    Last edited by rickbsgu; 4th March 2016 at 19:23.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by rickbsgu View Post
    Should this:

    Q_PROPERTY(QList<ChannelEntry> channels READ channels WRITE setChannels NOTIFY channelsChanged)
    be

    Q_PROPERTY(QList<ChannelEntry *> m_channels READ channels WRITE setChannels NOTIFY channelsChanged)

    ?
    Yes.

    Quote Originally Posted by rickbsgu View Post
    Also, I think all your properties need to point to the 'm_'<variableName>, where you've declared them as such.
    No.

    Quote Originally Posted by andrioli.n View Post
    My GridView is well populated but my ListView remains empty... Even after 2-3 days of trying different approach...

    So what am I missing ? Anyone has a clue, please ?
    Aside from the error spotted by rickbsgu which probably happend when you simplied the code for pasting, and the ever growing channel list which probably has the same cause, this looks quite OK.

    Normally you wouldn't depend on a certain type of model inside a delegate but that is OK as well.

    Is your channels getter function being called?
    Are ChannelEntry {} instance being created?

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    rickbsgu (4th March 2016)

  5. #4
    Join Date
    Mar 2016
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    First of all, thank you for your answers.

    Should this:
    Q_PROPERTY(QList<ChannelEntry> channels READ channels WRITE setChannels NOTIFY channelsChanged)
    be
    Q_PROPERTY(QList<ChannelEntry *> m_channels READ channels WRITE setChannels NOTIFY channelsChanged)
    ?
    Exact, but as anda_skoa said, it's due to simplification (it's a pointer in my real source code) but doesn't work anyway...

    By the way, in the constructor of my OverviewEntry object :

    Qt Code:
    1. m_overviewList.append(new OverviewEntry("bar", channels));
    To copy to clipboard, switch view to plain text mode 

    my variable channels (QList of ChannelEntry *) contains the right data. And the member m_channels is well filled by the same data.

    All is functionnal until it reached the QML part.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by andrioli.n View Post
    Exact, but as anda_skoa said, it's due to simplification (it's a pointer in my real source code) but doesn't work anyway...
    Posting obviously wrong code has the problem of everyone finding the obvious problems.
    Posting working code gives everyone a chance to spot the actual problem.

    So it is a matter of whether you want help finding problems that don't exist or finding problems that you have as well.

    Quote Originally Posted by andrioli.n View Post
    All is functionnal until it reached the QML part.
    Good! Then you can go ahead and try the suggested things to narrow the problem down.

    Cheers,
    _

  7. #6
    Join Date
    Mar 2016
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by anda_skoa View Post
    Posting obviously wrong code has the problem of everyone finding the obvious problems.
    Posting working code gives everyone a chance to spot the actual problem.

    So it is a matter of whether you want help finding problems that don't exist or finding problems that you have as well.
    Thanks for the advice, the posted code wasn't wrong on purpose but due to a typo...

    For trying the suggested things, I don't see any other leads. I checked your suggestions earlier but everything works good in this side of the code. I just can't access my embedded model in my QML. I'm stuck right there...

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by andrioli.n View Post
    For trying the suggested things, I don't see any other leads.
    I suggested to things to look into in comment #3, but I don't see where you've posted your findings.
    Maybe you can post the again?

    Cheers,
    _

  9. #8
    Join Date
    Mar 2016
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by anda_skoa View Post
    Normally you wouldn't depend on a certain type of model inside a delegate but that is OK as well.

    Is your channels getter function being called?
    Are ChannelEntry {} instance being created?
    My apologies, I only talk about my OverviewEntry instance in my response but the ChannelEntry instances contained in variable m_channels are well instantiated.

    Quote Originally Posted by andrioli.n View Post
    By the way, in the constructor of my OverviewEntry object :

    Qt Code:
    1. m_overviewList.append(new OverviewEntry("bar", channels));
    To copy to clipboard, switch view to plain text mode 
    my variable channels (QList of ChannelEntry *) contains the right data. And the member m_channels is well filled by the same data.

    All is functionnal until it reached the QML part.
    Any other suggestions ?

    Thank you

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by andrioli.n View Post
    My apologies, I only talk about my OverviewEntry instance in my response but the ChannelEntry instances contained in variable m_channels are well instantiated.
    Well, yes, you said that the C++ parts are working.
    I mean the ChannelEntry QML elements. Are they being instantiated as often as they should.

    Also, how did you determine that the property getter is being called?
    Debug output? Breakpoint? Is it called on all the overview objects?

    Cheers,
    _

  11. #10
    Join Date
    Mar 2016
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by anda_skoa View Post
    I mean the ChannelEntry QML elements. Are they being instantiated as often as they should.
    As I said in my first post, my GridView of OverviewEntry is well populated in QML but my ListView of ChannelEntry remains empty with a count of 0 element, even if the m_channels property contains my ChannelEntries.

    So it doesn't seems to be a matter of how QML is interpreting my ChannelEntries objects but the list in itself, I believe.

    Quote Originally Posted by anda_skoa View Post
    Also, how did you determine that the property getter is being called?
    Debug output? Breakpoint? Is it called on all the overview objects?
    Well, I put some breakpoint in my OverviewEntry and ChannelEntry constructors and they are well instantiated. In terms of data, all is there. I guess I miss some specific QT declaration for making my QList<ChannelEntry *> usable in a model's member...

  12. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by andrioli.n View Post
    Well, I put some breakpoint in my OverviewEntry and ChannelEntry constructors and they are well instantiated.
    We could safe some time if you tell me how often I should repeat the question on whether the property getter is being called before you deem it worthy to be answered, then I can just do that in a single posting.

    Unless of course you don't want to narrow down the problem?

    Cheers,
    _

  13. #12
    Join Date
    Mar 2016
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by anda_skoa View Post
    We could safe some time if you tell me how often I should repeat the question on whether the property getter is being called before you deem it worthy to be answered, then I can just do that in a single posting.

    Unless of course you don't want to narrow down the problem?

    Cheers,
    _
    I should read more carefully, my bad...

    You're right, the getters of my OverviewEntry are well called but none of the ChannelEntry's one. (by the way, the ChannelEntry object is used elsewhere in the code and qml and worked well in a usual usage)

  14. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Ok, so OverviewEntry::channel() is called for all instances of OverViewEntry?
    And each call returned a non-empty list?

    Cheers,
    _

  15. #14
    Join Date
    Mar 2016
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by anda_skoa View Post
    Ok, so OverviewEntry::channel() is called for all instances of OverViewEntry?
    And each call returned a non-empty list?

    Cheers,
    _
    Exactly, that's why I suspect I misunderstood the way Qt is working behind the scene.

  16. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Ok, so lets summarize:

    - Your OverviewEntry QML instances are created
    - They display the value of the name property
    - They call the channels() getter when assigning to the ListView's model property
    - The channels() getters return a correct, non-empty QList<ChannelEntry*>
    - The ListView counts are 0

    What kind of output do you get when you put this into the ListViews
    Qt Code:
    1. onModelChanged: console.log("model for " + modelData.name + " is " + model);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  17. #16
    Join Date
    Mar 2016
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by anda_skoa View Post
    Ok, so lets summarize:

    - Your OverviewEntry QML instances are created
    - They display the value of the name property
    - They call the channels() getter when assigning to the ListView's model property
    - The channels() getters return a correct, non-empty QList<ChannelEntry*>
    - The ListView counts are 0

    What kind of output do you get when you put this into the ListViews
    Qt Code:
    1. onModelChanged: console.log("model for " + modelData.name + " is " + model);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    I just made the test and this is the output :


    • qml: model for OverviewEntry 1 is QVariant(QList<ChannelEntry*>)
    • qml: model for OverviewEntry 2 is QVariant(QList<ChannelEntry*>)
    • qml: model for OverviewEntry 3 is QVariant(QList<ChannelEntry*>)
    • qml: model for OverviewEntry 4 is QVariant(QList<ChannelEntry*>)
    • qml: model for OverviewEntry 5 is QVariant(QList<ChannelEntry*>)
    • qml: model for OverviewEntry 6 is QVariant(QList<ChannelEntry*>)

  18. #17
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Hmm.
    You also have
    Qt Code:
    1. Q_DECLARE_METATYPE(QList<ChannelEntry*>);
    To copy to clipboard, switch view to plain text mode 
    have you tried adding
    Qt Code:
    1. Q_DECLARE_METATYPE(ChannelEntry*);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  19. #18
    Join Date
    Mar 2016
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by anda_skoa View Post
    Hmm.
    You also have
    Qt Code:
    1. Q_DECLARE_METATYPE(QList<ChannelEntry*>);
    To copy to clipboard, switch view to plain text mode 
    have you tried adding
    Qt Code:
    1. Q_DECLARE_METATYPE(ChannelEntry*);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    I tried but it doesn't change anything. Even with these two Q_DECLARE_METATYPE commented, it works same way.

    But if I comment :

    Qt Code:
    1. //Main.cpp
    2. ...
    3. qmlRegisterType<ChannelEntry>("ChannelEntry", 1, 0, "ChannelEntry");
    4. ...
    To copy to clipboard, switch view to plain text mode 

    The app crashed...

  20. #19
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Probably because you are now using the same name for the registered type and the QML element type.
    Change the second string argument here and maybe also use qmlRegisterUncreatableType()

    Cheers,
    _

  21. #20
    Join Date
    Mar 2016
    Posts
    12
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to dynamically populate a ListView within a GridView ?

    Quote Originally Posted by anda_skoa View Post
    Probably because you are now using the same name for the registered type and the QML element type.
    Change the second string argument here and maybe also use qmlRegisterUncreatableType()

    Cheers,
    _
    I tried by refactoring my C++ type ChannelEntry into QChannelEntry and setting :

    Qt Code:
    1. //Main.cpp
    2. ...
    3. qmlRegisterType<QChannelEntry>("ChannelEntry", 1, 0, "QChannelEntry");
    4. qmlRegisterUncreatableType<QChannelEntry>("ChannelEntry", 1, 0, "QChannelEntry","");
    5. ...
    To copy to clipboard, switch view to plain text mode 

    And the only change is the result of your debug output :

    • qml: model for OverviewEntry 1 is QVariant(QList<QChannelEntry*>)
    • qml: model for OverviewEntry 2 is QVariant(QList<QChannelEntry*>)
    • qml: model for OverviewEntry 3 is QVariant(QList<QChannelEntry*>)
    • qml: model for OverviewEntry 4 is QVariant(QList<QChannelEntry*>)
    • qml: model for OverviewEntry 5 is QVariant(QList<QChannelEntry*>)
    • qml: model for OverviewEntry 6 is QVariant(QList<QChannelEntry*>)


    Remark : I also tried by commenting qmlRegisterType or qmlRegisterUncreatableType but with no success

Similar Threads

  1. Replies: 0
    Last Post: 16th October 2015, 16:51
  2. GridView without reflow
    By shaolin in forum Qt Quick
    Replies: 15
    Last Post: 12th May 2014, 14:14
  3. Replies: 7
    Last Post: 9th September 2013, 08:31
  4. Usage of QTableView as gridview
    By ada10 in forum Newbie
    Replies: 7
    Last Post: 9th August 2010, 23:13
  5. Replies: 1
    Last Post: 14th October 2007, 10:09

Tags for this Thread

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.