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. [SOLVED]How to delete an item from the treeview by droping an item to pushbutton or to an group box
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]How to delete an item from the treeview by droping an item to pushbutton or to an group box

Scheduled Pinned Locked Moved General and Desktop
drag and droptreeview
54 Posts 4 Posters 22.7k Views 3 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.
  • mrjjM mrjj

    @Ratzz
    Hi
    Something must gone wrong. maybe.
    Tried it here and it does accept drops

    https://www.dropbox.com/s/7bkqatzm6vt1kmz/promotedgb.zip?dl=0

    RatzzR Offline
    RatzzR Offline
    Ratzz
    wrote on last edited by Ratzz
    #26

    @mrjj
    Yes your code is working . But do not know in my case its not working .
    You have used Listwidget and i have used list view . Does it matters??

    --Alles ist gut.

    1 Reply Last reply
    0
    • SGaistS SGaist

      Out of curiosity… Why are you creating so many widgets that should allow to delete items from your view using dnd ? It doesn't feel very intuitive nor following the guidelines of major OSs

      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by
      #27

      @SGaist
      Yes , My treeview is in between listview and groupbox . even if the listview items are dragged to the pushbutton_delete its getting deleted. which in my case should not. what is the best way to do ??

      --Alles ist gut.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #28

        Implement it in one of the usual ways:

        • React to the delete or backspace key
        • Have a delete button that will delete the currently selected row/column etc.
        • Have a dedicated column with e.g. a QPushButton to delete the row completely.

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

        RatzzR 1 Reply Last reply
        0
        • SGaistS SGaist

          Implement it in one of the usual ways:

          • React to the delete or backspace key
          • Have a delete button that will delete the currently selected row/column etc.
          • Have a dedicated column with e.g. a QPushButton to delete the row completely.
          RatzzR Offline
          RatzzR Offline
          Ratzz
          wrote on last edited by
          #29

          @SGaist
          I already have a delete button which deletes entire row.

          --Alles ist gut.

          RatzzR 1 Reply Last reply
          0
          • RatzzR Ratzz

            @SGaist
            I already have a delete button which deletes entire row.

            RatzzR Offline
            RatzzR Offline
            Ratzz
            wrote on last edited by
            #30

            @mrjj
            The code you shared is working (group box).

            --Alles ist gut.

            mrjjM 1 Reply Last reply
            0
            • RatzzR Ratzz

              @mrjj
              The code you shared is working (group box).

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #31

              @Ratzz
              Ok. super.

              RatzzR 1 Reply Last reply
              0
              • mrjjM mrjj

                @Ratzz
                Ok. super.

                RatzzR Offline
                RatzzR Offline
                Ratzz
                wrote on last edited by
                #32

                @mrjj
                But now i can drag items from treeview as well as listview to pushbutton and groupbox. But i do not want the user to allow listview items to dragged to pushbutton/groupbox

                --Alles ist gut.

                mrjjM 1 Reply Last reply
                0
                • RatzzR Ratzz

                  @mrjj
                  But now i can drag items from treeview as well as listview to pushbutton and groupbox. But i do not want the user to allow listview items to dragged to pushbutton/groupbox

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #33

                  @Ratzz
                  Hmm maybe you can check with
                  sender();
                  in dragEnterEvent
                  and if its the not a Treeview then reject the drop

                  like

                  QTreeView* theone= dynamic_cast<QTreeView*>(sender());
                     if( theone!= NULL ) 
                     { 
                        event->acceptProposedAction();
                     }
                  
                  RatzzR 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @Ratzz
                    Hmm maybe you can check with
                    sender();
                    in dragEnterEvent
                    and if its the not a Treeview then reject the drop

                    like

                    QTreeView* theone= dynamic_cast<QTreeView*>(sender());
                       if( theone!= NULL ) 
                       { 
                          event->acceptProposedAction();
                       }
                    
                    RatzzR Offline
                    RatzzR Offline
                    Ratzz
                    wrote on last edited by
                    #34

                    @mrjj
                    I do not have dragEnterEvent event yet! . we have a custom event using dropMimeData .

                    --Alles ist gut.

                    mrjjM 1 Reply Last reply
                    0
                    • RatzzR Ratzz

                      @mrjj
                      I do not have dragEnterEvent event yet! . we have a custom event using dropMimeData .

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #35

                      @Ratzz
                      how does this custom event look like ?

                      and its not an options just to disable Drag from listview?

                      RatzzR 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @Ratzz
                        how does this custom event look like ?

                        and its not an options just to disable Drag from listview?

                        RatzzR Offline
                        RatzzR Offline
                        Ratzz
                        wrote on last edited by Ratzz
                        #36

                        @mrjj
                        All drag drop events are handled in this. I am not aware how Bec, previous programmer has done it.

                        bool MessageModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
                        {
                            if(action == Qt::CopyAction)
                            {
                        //        QStringList format = data->formats();
                                QByteArray encodedData = data->data("application/x-qabstractitemmodeldatalist");
                                QDataStream stream(&encodedData, QIODevice::ReadOnly);
                                QMap<int,  QVariant> roleDataMap;
                        //        roleDataMap.setInsertInOrder(true);
                                QMap<int, QString> sortMsgs;
                        
                                while (!stream.atEnd())
                                {
                                    int row, col;
                                    stream >> row >> col >> roleDataMap;
                                    QString tmp = roleDataMap.value(0).toString();
                                    //MsgId1.append(tmp);
                                    sortMsgs.insert(row,tmp);
                                }
                                QList < QString > MsgId1 = sortMsgs.values();
                        
                                if(parent.parent().isValid() && !parent.parent().parent().isValid())
                                {
                                    QStandardItemModel::dropMimeData(data,action,row,column,parent);
                                    emit addFrameMessageIds(parent.row(), -1, MsgId1);
                                    return true;
                                }
                                if(parent.parent().parent().isValid() && column == -1)
                                {
                                    QStandardItemModel::dropMimeData(data,action,parent.row() +1,column,parent.parent());
                                    emit addFrameMessageIds(parent.parent().row(),parent.row() +1, MsgId1);
                                    return true;
                                }
                                if(parent.parent().parent().isValid() && column == 0)
                                {
                                    QStandardItemModel::dropMimeData(data,action,row,column,parent);
                                    emit addFrameMessageIds(parent.row(), row, MsgId1);
                                    return true;
                                }
                            }
                            else if(action == Qt::MoveAction)
                            {
                                if(!parent.isValid())
                                {
                                    return QStandardItemModel::dropMimeData(data,action,indexesLocal.row(),indexesLocal.column(),indexesLocal.parent());
                                }
                                if(indexesLocal.parent().parent().isValid() && !parent.parent().isValid() && column == -1)
                                {
                                    //Msg is dropped on major frame
                                    return QStandardItemModel::dropMimeData(data,action,indexesLocal.row(),indexesLocal.column(),indexesLocal.parent());
                                }
                                if(indexesLocal.parent().parent().isValid() && !parent.parent().parent().isValid() && column == -1)
                                {
                                    //Msg is dropped on minor frame
                                    if(indexesLocal.parent().row() != parent.row())
                                    {
                                        QStandardItemModel::dropMimeData(data,action,row,column,parent);
                                        emit moveFrameMessageIds(indexesLocal.parent().row(),parent.row(),indexesLocal.row(),-1);
                                        return true;
                                    }
                                    else
                                    {
                                        QStandardItemModel::dropMimeData(data,action, 0 ,column,parent);
                                        emit moveFrameMessageIds(indexesLocal.parent().row(),parent.row(),indexesLocal.row(),0);
                                        return true;
                                    }
                                }
                                if(indexesLocal.parent().parent().isValid() && parent.parent().parent().isValid() && column == -1)
                                {
                                    //      Msg is dropped on another msg
                                    QStandardItemModel::dropMimeData(data,action,parent.row()+1,column,parent.parent());
                                    emit moveFrameMessageIds(indexesLocal.parent().row(),parent.parent().row(),indexesLocal.row(),parent.row()+1);
                                    return true;
                                }
                                if(indexesLocal.parent().isValid() && !(indexesLocal.parent().parent().isValid()) && !parent.parent().isValid() && column == -1)
                                {
                                    //      Minor frame is dropped on Major frame
                                    QStandardItemModel::dropMimeData(data,action,0,indexesLocal.column(),indexesLocal.parent());
                                    emit moveFrame(indexesLocal.row(),0);
                                    return true;
                                }
                                if(indexesLocal.parent().isValid() && !(indexesLocal.parent().parent().isValid()) && parent.parent().isValid() && !(parent.parent().parent().isValid()) && column == -1)
                                {
                                    // Minor frame is dropped on another minor frame
                                    int pre = indexesLocal.row();
                                    int cur = parent.row();
                                    QStandardItemModel::dropMimeData(data,action,parent.row()+1,column,parent.parent());
                                    emit moveFrame(indexesLocal.row(),parent.row());
                                    return true;
                                }
                                if(indexesLocal.parent().isValid() && !(indexesLocal.parent().parent().isValid()) && parent.parent().parent().isValid() && column == -1)
                                {
                                    return QStandardItemModel::dropMimeData(data,action,indexesLocal.row(),indexesLocal.column(),indexesLocal.parent());
                                    //      Minor frame is dropped on msg
                                }
                                if(indexesLocal.parent().isValid() && !(indexesLocal.parent().parent().isValid()) && !parent.parent().isValid() && column == 0)
                                {
                                    QStandardItemModel::dropMimeData(data,action,row,indexesLocal.column(),indexesLocal.parent());
                                    emit moveFrame(indexesLocal.row(),row);
                                    return true;
                                    //      Minor frame is dropped after minor
                                }
                                if(indexesLocal.parent().isValid() && !(indexesLocal.parent().parent().isValid()) && !parent.parent().parent().isValid() && column == 0)
                                {
                                    return QStandardItemModel::dropMimeData(data,action,indexesLocal.row(),indexesLocal.column(),indexesLocal.parent());
                                    //      Minor frame is dropped after msg
                                }
                                if(indexesLocal.parent().parent().isValid() && parent.isValid() && !parent.parent().isValid() && column == 0)
                                {
                                    return QStandardItemModel::dropMimeData(data,action,indexesLocal.row(),indexesLocal.column(),indexesLocal.parent());
                                    //      Msg is dropped after minor
                                }
                                if(indexesLocal.parent().parent().isValid() && parent.parent().isValid() && !parent.parent().parent().isValid() && column == 0)
                                {
                                    QStandardItemModel::dropMimeData(data,action,row,parent.column(),parent);
                                    emit moveFrameMessageIds(indexesLocal.parent().row(),parent.row(),indexesLocal.row(),row);
                                    return true;
                                    //      Msg is dropped after msg
                                }
                        }
                        }
                        

                        And one more thing. If i drag items to pushbutton/group box the listview count is not getting updated.. ex:if i have 5 items and drag 1 item to pushbutton_delete then the count will be 5 . if i manually press delete button now then the list is getting updated to 3.

                        --Alles ist gut.

                        mrjjM 1 Reply Last reply
                        0
                        • RatzzR Ratzz

                          @mrjj
                          All drag drop events are handled in this. I am not aware how Bec, previous programmer has done it.

                          bool MessageModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
                          {
                              if(action == Qt::CopyAction)
                              {
                          //        QStringList format = data->formats();
                                  QByteArray encodedData = data->data("application/x-qabstractitemmodeldatalist");
                                  QDataStream stream(&encodedData, QIODevice::ReadOnly);
                                  QMap<int,  QVariant> roleDataMap;
                          //        roleDataMap.setInsertInOrder(true);
                                  QMap<int, QString> sortMsgs;
                          
                                  while (!stream.atEnd())
                                  {
                                      int row, col;
                                      stream >> row >> col >> roleDataMap;
                                      QString tmp = roleDataMap.value(0).toString();
                                      //MsgId1.append(tmp);
                                      sortMsgs.insert(row,tmp);
                                  }
                                  QList < QString > MsgId1 = sortMsgs.values();
                          
                                  if(parent.parent().isValid() && !parent.parent().parent().isValid())
                                  {
                                      QStandardItemModel::dropMimeData(data,action,row,column,parent);
                                      emit addFrameMessageIds(parent.row(), -1, MsgId1);
                                      return true;
                                  }
                                  if(parent.parent().parent().isValid() && column == -1)
                                  {
                                      QStandardItemModel::dropMimeData(data,action,parent.row() +1,column,parent.parent());
                                      emit addFrameMessageIds(parent.parent().row(),parent.row() +1, MsgId1);
                                      return true;
                                  }
                                  if(parent.parent().parent().isValid() && column == 0)
                                  {
                                      QStandardItemModel::dropMimeData(data,action,row,column,parent);
                                      emit addFrameMessageIds(parent.row(), row, MsgId1);
                                      return true;
                                  }
                              }
                              else if(action == Qt::MoveAction)
                              {
                                  if(!parent.isValid())
                                  {
                                      return QStandardItemModel::dropMimeData(data,action,indexesLocal.row(),indexesLocal.column(),indexesLocal.parent());
                                  }
                                  if(indexesLocal.parent().parent().isValid() && !parent.parent().isValid() && column == -1)
                                  {
                                      //Msg is dropped on major frame
                                      return QStandardItemModel::dropMimeData(data,action,indexesLocal.row(),indexesLocal.column(),indexesLocal.parent());
                                  }
                                  if(indexesLocal.parent().parent().isValid() && !parent.parent().parent().isValid() && column == -1)
                                  {
                                      //Msg is dropped on minor frame
                                      if(indexesLocal.parent().row() != parent.row())
                                      {
                                          QStandardItemModel::dropMimeData(data,action,row,column,parent);
                                          emit moveFrameMessageIds(indexesLocal.parent().row(),parent.row(),indexesLocal.row(),-1);
                                          return true;
                                      }
                                      else
                                      {
                                          QStandardItemModel::dropMimeData(data,action, 0 ,column,parent);
                                          emit moveFrameMessageIds(indexesLocal.parent().row(),parent.row(),indexesLocal.row(),0);
                                          return true;
                                      }
                                  }
                                  if(indexesLocal.parent().parent().isValid() && parent.parent().parent().isValid() && column == -1)
                                  {
                                      //      Msg is dropped on another msg
                                      QStandardItemModel::dropMimeData(data,action,parent.row()+1,column,parent.parent());
                                      emit moveFrameMessageIds(indexesLocal.parent().row(),parent.parent().row(),indexesLocal.row(),parent.row()+1);
                                      return true;
                                  }
                                  if(indexesLocal.parent().isValid() && !(indexesLocal.parent().parent().isValid()) && !parent.parent().isValid() && column == -1)
                                  {
                                      //      Minor frame is dropped on Major frame
                                      QStandardItemModel::dropMimeData(data,action,0,indexesLocal.column(),indexesLocal.parent());
                                      emit moveFrame(indexesLocal.row(),0);
                                      return true;
                                  }
                                  if(indexesLocal.parent().isValid() && !(indexesLocal.parent().parent().isValid()) && parent.parent().isValid() && !(parent.parent().parent().isValid()) && column == -1)
                                  {
                                      // Minor frame is dropped on another minor frame
                                      int pre = indexesLocal.row();
                                      int cur = parent.row();
                                      QStandardItemModel::dropMimeData(data,action,parent.row()+1,column,parent.parent());
                                      emit moveFrame(indexesLocal.row(),parent.row());
                                      return true;
                                  }
                                  if(indexesLocal.parent().isValid() && !(indexesLocal.parent().parent().isValid()) && parent.parent().parent().isValid() && column == -1)
                                  {
                                      return QStandardItemModel::dropMimeData(data,action,indexesLocal.row(),indexesLocal.column(),indexesLocal.parent());
                                      //      Minor frame is dropped on msg
                                  }
                                  if(indexesLocal.parent().isValid() && !(indexesLocal.parent().parent().isValid()) && !parent.parent().isValid() && column == 0)
                                  {
                                      QStandardItemModel::dropMimeData(data,action,row,indexesLocal.column(),indexesLocal.parent());
                                      emit moveFrame(indexesLocal.row(),row);
                                      return true;
                                      //      Minor frame is dropped after minor
                                  }
                                  if(indexesLocal.parent().isValid() && !(indexesLocal.parent().parent().isValid()) && !parent.parent().parent().isValid() && column == 0)
                                  {
                                      return QStandardItemModel::dropMimeData(data,action,indexesLocal.row(),indexesLocal.column(),indexesLocal.parent());
                                      //      Minor frame is dropped after msg
                                  }
                                  if(indexesLocal.parent().parent().isValid() && parent.isValid() && !parent.parent().isValid() && column == 0)
                                  {
                                      return QStandardItemModel::dropMimeData(data,action,indexesLocal.row(),indexesLocal.column(),indexesLocal.parent());
                                      //      Msg is dropped after minor
                                  }
                                  if(indexesLocal.parent().parent().isValid() && parent.parent().isValid() && !parent.parent().parent().isValid() && column == 0)
                                  {
                                      QStandardItemModel::dropMimeData(data,action,row,parent.column(),parent);
                                      emit moveFrameMessageIds(indexesLocal.parent().row(),parent.row(),indexesLocal.row(),row);
                                      return true;
                                      //      Msg is dropped after msg
                                  }
                          }
                          }
                          

                          And one more thing. If i drag items to pushbutton/group box the listview count is not getting updated.. ex:if i have 5 items and drag 1 item to pushbutton_delete then the count will be 5 . if i manually press delete button now then the list is getting updated to 3.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #37

                          @Ratzz said:

                          But i do not want the user to allow listview items to dragged to pushbutton/groupbox

                          So why can you not check there ?

                          also
                          http://doc.qt.io/qt-5/qtreewidget.html#dropMimeData
                          Seems to be when something is dropped on Tree and im confused what it has to do with the
                          Groupbox ?

                          RatzzR 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @Ratzz said:

                            But i do not want the user to allow listview items to dragged to pushbutton/groupbox

                            So why can you not check there ?

                            also
                            http://doc.qt.io/qt-5/qtreewidget.html#dropMimeData
                            Seems to be when something is dropped on Tree and im confused what it has to do with the
                            Groupbox ?

                            RatzzR Offline
                            RatzzR Offline
                            Ratzz
                            wrote on last edited by
                            #38

                            @mrjj
                            Since the groupbox is deletes item as you suggested it also deletes listview items when dragged.

                            --Alles ist gut.

                            mrjjM 1 Reply Last reply
                            0
                            • RatzzR Ratzz

                              @mrjj
                              Since the groupbox is deletes item as you suggested it also deletes listview items when dragged.

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #39

                              @Ratzz
                              And the DragGBox do have
                              void dragEnterEvent(QDragEnterEvent* event) {
                              event->acceptProposedAction();
                              }

                              So why cant you just check that sender if the accepted one and then acccept or if not then
                              reject the drop?

                              RatzzR 1 Reply Last reply
                              0
                              • mrjjM mrjj

                                @Ratzz
                                And the DragGBox do have
                                void dragEnterEvent(QDragEnterEvent* event) {
                                event->acceptProposedAction();
                                }

                                So why cant you just check that sender if the accepted one and then acccept or if not then
                                reject the drop?

                                RatzzR Offline
                                RatzzR Offline
                                Ratzz
                                wrote on last edited by
                                #40

                                @mrjj
                                dragEnterEvent shoud be used for main class?

                                --Alles ist gut.

                                mrjjM 1 Reply Last reply
                                0
                                • RatzzR Ratzz

                                  @mrjj
                                  dragEnterEvent shoud be used for main class?

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #41

                                  @mrjj said:
                                  For the DropGroupBox class.
                                  I assume you use that one?

                                  RatzzR 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @mrjj said:
                                    For the DropGroupBox class.
                                    I assume you use that one?

                                    RatzzR Offline
                                    RatzzR Offline
                                    Ratzz
                                    wrote on last edited by Ratzz
                                    #42

                                    @mrjj
                                    yes i have used this one.
                                    Since its different class how to pull the sender to it?
                                    can you just drop me some piece of code.

                                    --Alles ist gut.

                                    1 Reply Last reply
                                    0
                                    • ? Guest
                                      RatzzR Offline
                                      RatzzR Offline
                                      Ratzz
                                      wrote on last edited by
                                      #43

                                      @mrjj
                                      Not it does not allow even the treeview items to drop

                                      --Alles ist gut.

                                      1 Reply Last reply
                                      0
                                      • ? Guest
                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #44

                                        STOP
                                        sender() not working for events it seems.
                                        So cannot work.
                                        So its a no go.

                                        RatzzR 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          STOP
                                          sender() not working for events it seems.
                                          So cannot work.
                                          So its a no go.

                                          RatzzR Offline
                                          RatzzR Offline
                                          Ratzz
                                          wrote on last edited by
                                          #45

                                          @mrjj
                                          ok fine.. what next?!

                                          --Alles ist gut.

                                          mrjjM 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