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. [SOLVED] ListView drag and drop, data model exposed from C++
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] ListView drag and drop, data model exposed from C++

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 4 Posters 7.8k 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.
  • B Offline
    B Offline
    borut123
    wrote on last edited by
    #1

    Hi

    This is a great example of how to implement drag and drop in QML:
    http://developer.qt.nokia.com/wiki/Drag_and_Drop_within_a_GridView

    Would it be also possible to implement similar feature if I use the data model exposed from C++
    ( context->setContextProperty("listItems", QVariant::fromValue(listItems)) ),
    and not the data model defined in QML (via ListModel)?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      Yes. You need to implement some methods marked as Q_INVOKABLE to move elements, but I'm sure it not too difficult.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        borut123
        wrote on last edited by
        #3

        Ok, but if you look at the example code, "icons.move(newIndex, newIndex = index, 1)" is used in QML javascript.

        icons is defined as: " model: WidgetModel { id: icons }"

        in my case I'm using model exposed from C++, so it's: "model: myModel"

        And there I can not use QML javascript code "myModel.move() to move the item", or maybe I can?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

          you can use "myModel.move()" from js if you have move() method marked as Q_INVOKABLE or slot.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            borut123
            wrote on last edited by
            #5

            Thanks, after implementing list model from QAbstractListModel, and of course also move() method, everything works fine.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DenisKormalev
              wrote on last edited by
              #6

              Don't forget to mark your own threads which you think are solved with "[solved]" in title.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                carlossst
                wrote on last edited by
                #7

                Hello!

                I got exactly the same issue as borut123, and from your explanations I understand:
                To be able to use myModel.move() in QML I need to implement in C++ move() by using Q_INVOKABLE.

                But:

                1. After reading QML and C++ documentation, Q_INVOKABLE is needed to create NEW methods in C++ (not previously existing in QML), then make them visible to QML and next use them in QML.
                2. move() method is a property ALREADY defined in QML as ListModel::move ( int from, int to, int n ).

                The question is:
                how exactly I have to use Q_INVOKABLE with move() to enable usage of myModel.move() in QML? Because I should need to declare in the C++ model class? Then: Is it needed to reimplement move() in .cpp also?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  borut123
                  wrote on last edited by
                  #8

                  You need to implement QAbstractListModel, and then send the instance of this model to QML.

                  C++ header file code snippet:

                  @class YourObject : public QAbstractListModel {

                  Q_OBJECT
                  

                  public:
                  int rowCount(const QModelIndex & parent = QModelIndex()) const;
                  Q_INVOKABLE void move(int from, int to, int count);
                  Q_INVOKABLE YourObject* get(int i) const;
                  QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;

                  @
                  I hope this helps you a little bit.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jackyang
                    wrote on last edited by
                    #9

                    Hi borut123,

                    I saw you reimplement "get" function of model in c++ header file.

                    Does its return value YourObject is your model's pointer?

                    Should it be the pointer of item which model stored, not model's ?

                    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