Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. using QList from c++ backend in QML
Forum Updated to NodeBB v4.3 + New Features

using QList from c++ backend in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 2.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Allerknappe
    wrote on 25 Mar 2018, 18:23 last edited by
    #1

    Hello Guys,

    i´ve tried to put my own backend c++ code in a QML model.
    i write a new class with this code:

    class DataObject : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
        Q_PROPERTY(QString detailText READ detailText WRITE setDetailText NOTIFY detailTextChanged)
     ...
    

    i add then with:
    QList dataList;
    dataList.append(new DataObject(chat->kon[i]->getname(), QVariant(ia).toString()));

    a new QList.

    so far so good.
    Now i add in the main.cpp with:

    engine.rootContext()->setContextProperty("modelContact", QVariant::fromValue(cont->dataList));

    a modelContact property.

    so in the example code it´s in the qml code the following area that i want to change:

    // Model should be loaded from your messaging backend
            //  model: [
            //    { text: "Tom McEloy", detailText: "Sorry for the late reply ...", image: Qt.resolvedUrl("../../assets/portrait0.jpg") },
            //    { text: "Leah Douglas", detailText: "Hahaha :D", image: Qt.resolvedUrl("../../assets/portrait1.jpg") }
            //  ]
    

    so like the comment i want to load the model from the backend, so i try:

    model: modelContact
    

    but i get no text or detailtext, where is the problem?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Mar 2018, 20:07 last edited by
      #2

      Hi,

      Are you following the Using C++ Models with Qt Quick Views chapter of Qt's documentation ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Allerknappe
        wrote on 25 Mar 2018, 21:48 last edited by
        #3

        yeah i used exactly this documentation.
        i think that the model is correctly loaded, but how can i change this code

         // Model should be loaded from your messaging backend
                //  model: [
                //    { text: "Tom McEloy", detailText: "Sorry for the late reply ...", image: Qt.resolvedUrl("../../assets/portrait0.jpg") },
                //    { text: "Leah Douglas", detailText: "Hahaha :D", image: Qt.resolvedUrl("../../assets/portrait1.jpg") }
                //  ]
        

        with the modelData object from the Using C++ Models with Qt Quick Views chapter documentation.
        if i try this code

         model : [ { text: model.modelData.text } ]
        

        nothing happen...

        R 1 Reply Last reply 27 Mar 2018, 07:04
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 26 Mar 2018, 21:47 last edited by
          #4

          Then I think you have to follow the Exposing C++ Data Model to QML.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Allerknappe
            25 Mar 2018, 21:48

            yeah i used exactly this documentation.
            i think that the model is correctly loaded, but how can i change this code

             // Model should be loaded from your messaging backend
                    //  model: [
                    //    { text: "Tom McEloy", detailText: "Sorry for the late reply ...", image: Qt.resolvedUrl("../../assets/portrait0.jpg") },
                    //    { text: "Leah Douglas", detailText: "Hahaha :D", image: Qt.resolvedUrl("../../assets/portrait1.jpg") }
                    //  ]
            

            with the modelData object from the Using C++ Models with Qt Quick Views chapter documentation.
            if i try this code

             model : [ { text: model.modelData.text } ]
            

            nothing happen...

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 27 Mar 2018, 07:04 last edited by
            #5

            @Allerknappe
            QML can convert QLists using basic data types. For lists containing Items or QObjects you need to implement a QQuickListProperty property. See the birthday party example as reference.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            G 1 Reply Last reply 27 Mar 2018, 09:05
            0
            • R raven-worx
              27 Mar 2018, 07:04

              @Allerknappe
              QML can convert QLists using basic data types. For lists containing Items or QObjects you need to implement a QQuickListProperty property. See the birthday party example as reference.

              G Offline
              G Offline
              GrecKo
              Qt Champions 2018
              wrote on 27 Mar 2018, 09:05 last edited by GrecKo
              #6

              A QQmlListProperty is needed when you want to populate a list declaratively from QML.

              When you data is coming from c++, you can expose a list with a QList<QObject*> (but not a QList<DerivedFromQObject*>), a QVarantList or a QAbstractItemModel (a QAbstractListModel generally). The latter is more generic, allow more fine grained changes to the model and is more type safe.

              Implementing a QAbstractItemModel can be a tedious task, if you don't want to do it yourself I recommend one of those 2 librairies : http://gitlab.unique-conception.org/qt-qml-tricks/qt-qml-models & https://github.com/benlau/qsyncable

              1 Reply Last reply
              0

              1/6

              25 Mar 2018, 18:23

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved