Results 1 to 16 of 16

Thread: Using list model in C++ code with UI in QML

  1. #1
    Join Date
    Nov 2010
    Posts
    8
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Using list model in C++ code with UI in QML

    Hello,
    I am writing an application that fetches few items from the symbian database and adds them to a list view that is shown using QML.
    I am using listview in my qml file and i have named the model to that list view as "listModel"
    Hence, I am initialising it in C++ code like this:

    QDeclarativeContext *context = view->rootContext();

    context->setContextProperty("listModel",QVariant::fromValu e(mItemsList));

    where, mItemsList is a QList of items.

    Things are working fine till now. Now, when I get a notification that new item has been added, I want to update the model.
    Right now, am again calling:

    context->setContextProperty("listModel",QVariant::fromValu e(mAlarmsList));

    This will again set the new model to the list view. This is working fine.
    But, I would like to know, if this is the only way to update the model.
    Is there a way to append/insert just newly added item to the already existing model in my C++ code?

    One more thing -> As you all might know, listview has a property called "currentIndex".
    If I want to manipulate this property of the list view that is defined in qml, how can I do that?

    Regards
    Chiru

  2. #2
    Join Date
    Dec 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Maemo/MeeGo

    Default Re: Using list model in C++ code with UI in QML

    Hi Chiru ,
    Can u pls explain a little more,I have also created 1 list,it needs to be updated when any item is added/removed or edited.
    I am also calling setContextProperty again.But list is not getting updated.
    Please paste few lines of code to understand in better way.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using list model in C++ code with UI in QML

    You do not really have a model, you just have a static list of items that acts as a model. You can implement your model in the QAbstractItemModel infrastructure and set your qml item's model to that. Then every update of this model in C++ should immediately be reflected in qml.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Nov 2010
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using list model in C++ code with UI in QML

    How to expose item's data from such model to QML's delegate.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using list model in C++ code with UI in QML

    Read the docs about Using QML in C++ Applications.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Nov 2010
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using list model in C++ code with UI in QML

    Let me provide example of what I'm trying to do.
    Qt Code:
    1. // addressbook.cpp
    2. AddressBook::AddressBook(QObject *parent) :
    3. {
    4. setQuery("select * from person");
    5. setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    6. setHeaderData(1, Qt::Horizontal, QObject::tr("firstName"));
    7. setHeaderData(2, Qt::Horizontal, QObject::tr("lastName"));
    8. }
    9.  
    10. // main.cpp
    11. int main(int argc, char *argv[]) {
    12. // ...
    13. AddressBook addressBook;
    14. rootContex->setContextProperty("addressBook", &addressBook);
    15. // ...
    16. }
    17.  
    18. // main.qml
    19. import Qt 4.7
    20.  
    21. Rectangle {
    22. // ...
    23.  
    24. ListView {
    25. id: list
    26.  
    27. anchors.fill: parent
    28. model: addressBook
    29. delegate: Item {
    30. Column {
    31. Text {
    32. text: firstname
    33. }
    34. Text {
    35. text: lastName
    36. }
    37. }
    38. }
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using list model in C++ code with UI in QML

    And the problem is...?

    Hint: There is an example in examples/declarative/modelviews that shows you how to use a c++ model with qml.

    Another hint: ListView is a single column view.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Nov 2010
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using list model in C++ code with UI in QML

    Quote Originally Posted by wysota View Post
    And the problem is...?
    ...that it can't reference variables firstname and lastName.

    Quote Originally Posted by wysota View Post
    Another hint: ListView is a single column view.
    What does this have to do with my example?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using list model in C++ code with UI in QML

    Quote Originally Posted by delor View Post
    ...that it can't reference variables firstname and lastName.

    What does this have to do with my example?
    Exactly that. It can't reference those variables because you think that settng header names for model columns implicitly binds them with QML variables which is not the case. You can only accessroles of a single column from within Qt Quick's ListView element and you have to tell QtQuick what the roles are called. To use a multicolumn model with ListView you need a proxy model that will turn single role multiple column model into single column multiple role model.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Nov 2010
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using list model in C++ code with UI in QML

    How about this then:
    Qt Code:
    1. // addressbook.cpp
    2. AddressBook::AddressBook(QObject *parent) :
    3. {
    4. setQuery("select * from person");
    5.  
    6. QHash<int, QByteArray> roles;
    7. roles[FirstName] = "firstName";
    8. roles[LastName] = "lastName";
    9. setRoleNames(roles);
    10. }
    11.  
    12. QVariant AddressBook::data(const QModelIndex &item, int role) const
    13. {
    14. QSqlRecord *r = reinterpret_cast<QSqlRecord*>(item.internalPointer());
    15.  
    16. if (role == FirstName)
    17. return r->field("firstname").value();
    18. if (role == LastName)
    19. return r->field("lastName").value();
    20. return QVariant();
    21. }
    To copy to clipboard, switch view to plain text mode 
    item.internalPointer() returns null pointer all the time.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using list model in C++ code with UI in QML

    Why would it return a valid QSqlRecord pointer?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Nov 2010
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Using list model in C++ code with UI in QML

    This worked out just fine:
    Qt Code:
    1. QVariant AddressBook::data(const QModelIndex &item, int role) const
    2. {
    3. switch (role) {
    4. case FirstName:
    5. return record(item.row()).field("firstname").value();
    6. break;
    7. case LastName:
    8. return record(item.row()).field("lastName").value();
    9. break;
    10. default:
    11. return QSqlQueryModel::data(item, role);
    12. break;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using list model in C++ code with UI in QML

    I made a quick implementation of a general way to solve this situation:

    [wiki]QMLifyProxyModel[/wiki]
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14

    Default Re: Using list model in C++ code with UI in QML

    Hi,

    QMLifyProxyModel works quite good for me.
    Just one bug correction on it :

    The function QMLifyProxyModel::rowCount is actually returning columnCount.
    Need to modify it :

    Qt Code:
    1. int QMLifyProxyModel::rowCount(const QModelIndex &parent) const
    2. {
    3. if(!sourceModel())
    4. return 0;
    5. if(parent.isValid())
    6. return 0;
    7.  
    8. return sourceModel()->rowCount();
    9. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Aug 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using list model in C++ code with UI in QML

    can you show all structure of connection list model in C++ code with UI in QML???
    thanks
    i have the same issue

  16. #16

    Default Re: Using list model in C++ code with UI in QML

    Quote Originally Posted by olegdim View Post
    can you show all structure of connection list model in C++ code with UI in QML???
    Sorry, I don't have it anymore, it was in my previous company one year ago.

Similar Threads

  1. Flags of a list model
    By woodtluk in forum Qt Programming
    Replies: 4
    Last Post: 8th October 2010, 12:45
  2. Qt Creator qt creator code model
    By Vanir in forum Qt Tools
    Replies: 0
    Last Post: 5th October 2010, 12:10
  3. Replies: 8
    Last Post: 6th May 2010, 12:17
  4. How to map tree model data to list view
    By msopanen in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2009, 20:56
  5. [QT4.1.1 XP] multi-columns list model/view
    By incapacitant in forum Newbie
    Replies: 1
    Last Post: 26th February 2006, 14:38

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.