Results 1 to 8 of 8

Thread: Display Data from C++ to QML

  1. #1
    Join Date
    Jan 2016
    Posts
    54
    Thanks
    7
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Wink Display Data from C++ to QML

    Hi EveryOne,

    I have a C++ class that does some operations
    The method : getDataFromServer() exports some Data from server, it works well
    Qt Code:
    1. for (int i = 0; i < projects.size(); ++i)
    2. {
    3. qDebug() << "result : " << projects[i];
    4. }
    To copy to clipboard, switch view to plain text mode 

    I want to display data in Listview QML

    QML code
    Qt Code:
    1. Rectangle
    2. {
    3. Component.onCompleted: {
    4. ClassTest.getDataFromServer();
    5. }
    6.  
    7. ClassTest{
    8. id: classTest
    9. }
    10.  
    11. Item {
    12. width: 800
    13. height: 600
    14.  
    15. ListView {
    16. id: listView
    17. anchors.left: parent.left
    18. anchors.top: parent.top
    19. anchors.bottom: parent.bottom
    20. width: 0.25 * parent.width
    21.  
    22. focus: true
    23. model: myModel
    24. delegate: Item {
    25.  
    26. width: parent.width
    27. height: 22
    28.  
    29. Text {
    30. anchors.centerIn: parent
    31. text: "data"
    32. }
    33.  
    34. MouseArea {
    35. anchors.fill: parent
    36. onClicked: {
    37. console.log(model.index)
    38. }
    39.  
    40. }
    41. }
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

    getDataFromServer() code
    Qt Code:
    1. ...
    2. // getData OK
    3. for (int i = 0; i < projects.size(); ++i)
    4. {
    5. qDebug() << "result : " << projects[i];
    6. }
    7. QQuickView *view= new QQuickView;
    8. QQmlContext *c = view->engine()->rootContext();
    9. c->setContextProperty("myModel", QVariant::fromValue(projects));
    To copy to clipboard, switch view to plain text mode 

    Any help plz

    Cheers,

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

    Default Re: Display Data from C++ to QML

    Your getDataFromServer() creates a new view?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2016
    Posts
    54
    Thanks
    7
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Display Data from C++ to QML

    The object is to display Data in QML

    I created a new QML file code above , I would like to communicate C++ class and This QML file, so I tried to use :
    Qt Code:
    1. QQuickView *view= new QQuickView;
    2. QQmlContext *c = view->engine()->rootContext();
    3. c->setContextProperty("myModel", QVariant::fromValue(projects));
    To copy to clipboard, switch view to plain text mode 

    Is this an other way simple to do that ??

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

    Default Re: Display Data from C++ to QML

    Quote Originally Posted by Binary01 View Post
    The object is to display Data in QML
    If you create another view, it is another view.
    In C++ two instance of a class are two different objects, with their owns state/data.

    Quote Originally Posted by Binary01 View Post
    Is this an other way simple to do that ??
    Either a property on an object exposed to QML or a model exposed to QML.
    What's the element type of "projects"?

    Cheers,
    _

  5. #5
    Join Date
    Jan 2016
    Posts
    54
    Thanks
    7
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Display Data from C++ to QML

    projects is a QStringList : QStringList projects

    Yeah I used a model exposed to QML : myModel

    I tried to use the example : http://doc.qt.io/qt-5/qtquick-modelv...cppmodels.html

    Cheers,
    Last edited by Binary01; 19th April 2016 at 14:53.

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

    Default Re: Display Data from C++ to QML

    Quote Originally Posted by Binary01 View Post
    projects is a QStringList : QStringList projects
    Ah, good, easy

    Quote Originally Posted by Binary01 View Post
    Yeah I used a model exposed to QML : myModel
    Currently you are not, your non-working code exports the QStringList as a context property.

    I assume "ClassTest" is a custom C++ type that you have registered with the QML type system?

    Just add a QStringList property to it and bind the ListView's model to that instead of myModel.
    Qt Code:
    1. ClassTest {
    2. id: classTest
    3. }
    4.  
    5. ListView {
    6. model: classTest.projects // assuming you give your property the name "projects"
    7. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. #7
    Join Date
    Jan 2016
    Posts
    54
    Thanks
    7
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Display Data from C++ to QML

    Yeah, "ClassTest" is a custom C++ type registered with the QML type system

    Is it necessary to add a new property ??

    In ClassTest, I added projects
    Qt Code:
    1. Q_PROPERTY(QStringList projects READ projects)
    To copy to clipboard, switch view to plain text mode 

    In QML
    Qt Code:
    1. ListView {
    2. model: classTest.projects
    3. }
    To copy to clipboard, switch view to plain text mode 

    In C++
    Qt Code:
    1. // getDataFromServer() == Get data From server
    2. for (int i = 0; i < projects.size(); ++i)
    3. {
    4. qDebug() << "result : " << projects[i];
    5. }
    6.  
    7. QQuickView *view= new QQuickView;
    8. QQmlContext *ctxt = view->engine()->rootContext();
    9. ctxt->setContextProperty("myModel", QVariant::fromValue(projects));
    To copy to clipboard, switch view to plain text mode 

    Still not work
    C++ code should remain as such it is ??

    Cheers,

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

    Default Re: Display Data from C++ to QML

    Quote Originally Posted by Binary01 View Post
    Yeah, "ClassTest" is a custom C++ type registered with the QML type system
    Great, that makes everything easy.

    Quote Originally Posted by Binary01 View Post
    Is it necessary to add a new property ??
    That's the easiest way.
    Do you need a more complicated approach?

    Quote Originally Posted by Binary01 View Post
    In ClassTest, I added projects
    Qt Code:
    1. Q_PROPERTY(QStringList projects READ projects)
    To copy to clipboard, switch view to plain text mode 
    Needs a NOTIFY signal, since the value is changing after object creation.

    Quote Originally Posted by Binary01 View Post
    In QML
    Qt Code:
    1. ListView {
    2. model: classTest.projects
    3. }
    To copy to clipboard, switch view to plain text mode 
    Exactly.

    Quote Originally Posted by Binary01 View Post
    In C++
    Qt Code:
    1. // getDataFromServer() == Get data From server
    2. for (int i = 0; i < projects.size(); ++i)
    3. {
    4. qDebug() << "result : " << projects[i];
    5. }
    6.  
    7. QQuickView *view= new QQuickView;
    8. QQmlContext *ctxt = view->engine()->rootContext();
    9. ctxt->setContextProperty("myModel", QVariant::fromValue(projects));
    To copy to clipboard, switch view to plain text mode 
    We've already determined that this is very wrong, no?

    Once the value of the property has changed, you emit the change notifcation signal.

    Cheers,
    _

Similar Threads

  1. Methods to display received data
    By pupqt in forum Qt Programming
    Replies: 3
    Last Post: 18th April 2011, 09:50
  2. Display DIB data
    By nightroad in forum Qt Programming
    Replies: 2
    Last Post: 29th December 2010, 21:42
  3. QPixmap Display RGB data
    By phoenixtju in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2009, 10:31
  4. Display data from QStandardItemModel in QTreeView
    By jprice01801 in forum Qt Programming
    Replies: 7
    Last Post: 10th January 2007, 09:34

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.