Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drag and Drop in ItemView dependant on Item

Drag and Drop in ItemView dependant on Item

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 821 Views
  • 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.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on last edited by
    #1

    Hello,

    I have a QTreeView with a model where I want to be able to drag and drop Items of type A onto Items of type B, and also Items of type C onto items of type D.

    I've implemented drag and drop for items of type A to B by returning Qt::ItemIsDragEnabledflags for A and Qt::ItemIsDropEnabled flags for B.

    However I do not know how I can switch the Qt::ItemIsDropEnabled flag depending on the item that is currently dragged around? What would be the right way to implement this?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      You should implement a method to change the Qt::ItemIsDropEnabled flag for an index in the model (e.g MyModel::setFlags(const QModelIndex&,Qt::ItemFlags) basically the same as QStandardItem::setFlags for QStandardItemModel) then do something like this:

      class MyView : public QTreeView {
          Q_OBJECT
          Q_DISABLE_COPY(MyView)
      public:
          MyView(QWidget *parent = nullptr) 
              : QTreeView (parent)
          {}
      protected:
          void dragMoveEvent(QDragMoveEvent *event) override{
              const QModelIndex index = indexAt(event->pos());
              const bool canBeDropped = checkIfItemDraggedCanBeDroppedOnIndex(index); // this depends on your logic
              const Qt::ItemFlags oldFlags = model()->flags(index);
              MyModel* mdl = nullptr;
              if(!canBeDropped && index.isValid() && (oldFlags & Qt::ItemIsDropEnabled)){
                  mdl = dynamic_cast<MyModel*>(model());
                  Q_ASSERT(mdl);
                  mdl->setFlags(index,oldFlags & ~Qt::ItemIsDropEnabled);
              }
              QTreeView::dragMoveEvent(event);
              if(mdl)
                  mdl->setFlags(index,oldFlags);
          }
      };
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by
        #3

        Thanks, that brought me a step further, but I still don't get how I find the item that is currently drag and dropped inside checkIfItemDraggedCanBeDroppedOnIndex(index) because the results depends on the type of that item.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          index is the model index of the destination item if what you don't know what the source item is then you can access its data as described here

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2
          • gde23G Offline
            gde23G Offline
            gde23
            wrote on last edited by
            #5

            Yeah, I mean the source item.
            Since I didn't want to mess around with the mime data I now ended up with another approach:
            In the mimeData() method of my model I set a boolen flag on the item that is currently the one that is dragged around. Now in the flags() method I get the flagged item from the model and look what type it is and depending on that I can decide whether drops are enabled on the underlying item or not. This works fine, however it still looks like a bit of a hack.

            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