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. How to Access ListModel from C++
Forum Updated to NodeBB v4.3 + New Features

How to Access ListModel from C++

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 4 Posters 1.0k Views 1 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.
  • K Offline
    K Offline
    Kenz Dale
    wrote on last edited by
    #1

    I have found many posts related to getting custom models into QML, but in my case I am very happy with the ListModel and simply want to make it available to C++. However, I am running into a crash which gives no feedback and I'm at a bit of a loss for how to continue.

    Here is the relevant code snippet from how I try accessing the model from C++

    C++:

    Q_PROPERTY(QAbstractListModel* model READ model WRITE setModel)
    QAbstractListModel* model() const {return model_;};
    void setModel(QAbstractListModel *model){};
    
    QAbstractListModel* model_;
    

    QML:

    ListModel {
        id: textModel
    }
    
    TextBalloon {
        model: textModel
    }
    

    Output:

    QML debugging is enabled. Only use this in a safe environment.
    05:38:08: The program has unexpectedly finished.
    

    Very frustrating not to have a more useful error message. It just bombs out. The debugger is no help, it gets stuck on view.setSource(QUrl("qrc:/qml/main.qml"));.

    The program loads correctly when I remove the model: textModel line.

    Anyone have a working example of accessing the ListModel from C++?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Best thing is to avoid accessing qml objects from c++. If you do some mistake it will crash like this. You have go deep inside object tree to find your list model and then access it

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • M Offline
        M Offline
        MrShawn
        wrote on last edited by
        #3

        See if this helps.

        https://www.youtube.com/watch?v=9BcAYDlpuT8

        1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          Can you post a simple example where your code crashes so that we can reproduce it ?
          What is the body of your setModel method ?

          Accessing QML objects like that is perfectly fine and not a bad practice since you are passing it to c++, it's not the c++ retrieving it with a findChild call.

          The video linked is about using a c++ model in a QML view, that's not relevant to OP's problem.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kenz Dale
            wrote on last edited by
            #5

            I'm attributing this to the build system. The problem resolved itself when I started from a blank generic example and copied over the code before putting it back into the problematic app.

            The only thing I can think of is that perhaps I needed to do a make clean.

            1 Reply Last reply
            0

            • Login

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