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

Send ListModel from QML to C++

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 3.1k 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.
  • S Offline
    S Offline
    ssudhindrarao
    wrote on last edited by
    #1

    HI,

    I need help in saving data to database via C++ from the QML data list.

    In my screen I have 1 combo box for Item selection, then 1 text box for quantity and a add button.

    Add button will add the items to dataModel, i.e. my TableView model

    dataModel.append({"id": cmbProductId.currentText ,"quantity":txtQuantity.text})

    Now the TableView is showing the added data correctly.

    Upon Save, I want to know how to pass the dataModel to my C++ function , and how can i access the values in C++ function.

    I have written an Q_INVOKABLE bool saveData();

    But i am not getting what should be the parameter for this function and how to access the dataModel values in CPP

    Thanks in advance

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stevenceuppens
      wrote on last edited by
      #2

      Hi,

      I would create a Model in c++ (inherited from QAbstractListModel)

      Provide an implementation of setData() in the model,

      And expose the model to QML (and to your TableView)

      Info:
      http://qt-project.org/doc/qt-5/qabstractlistmodel.html

      look at the subclass section,

      Steven CEUPPENS
      Developer / Architect
      Mobile: +32 479 65 93 10

      1 Reply Last reply
      0
      • S Offline
        S Offline
        ssudhindrarao
        wrote on last edited by
        #3

        Hi,
        Thanks for the solution. I am implementing that.

        By doing that, i need to call setData() eveytime i add an item to list model.

        Is there any way so that after adding all items , say 10 items, I can send the model to C++ function at once?

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on last edited by
          #4

          setData is just to update an already existing data row I guess, if you want to add new data you should implement your own function/slot for that I think and you can do what you want in there, if you want add 10 rows at once or whatever. You just have to emit the proper signals so the views knows that you have added some new rows, see here: http://qt-project.org/doc/qt-5/qabstractitemmodel.html#beginInsertRows

          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