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


  • Qt Champions 2017

    @Ratzz
    how does this custom event look like ?

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



  • @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.


  • Qt Champions 2017

    @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 ?



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


  • Qt Champions 2017

    @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?



  • @mrjj
    dragEnterEvent shoud be used for main class?


  • Qt Champions 2017

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



  • @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.



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


  • Qt Champions 2017

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



  • @mrjj
    ok fine.. what next?!


  • Qt Champions 2017

    @Ratzz
    you can use
    QWidget * QDropEvent::source() const

    read here
    http://doc.qt.io/qt-5.5/qdropevent.html#source



  • @mrjj
    this way?

        void dragEnterEvent(QDragEnterEvent* event) {
            QWidget * theone= dynamic_cast<QWidget*>(event->source());
            if( theone == NULL )
            {
                event->acceptProposedAction();
            }
        }

  • Qt Champions 2017

    @Ratzz
    almost but must be the type you want to allow
    QTreeView * theone= dynamic_cast<QTreeView*>(event->source());
    if( theone != NULL )
    {
    event->acceptProposedAction();
    }

    so it says if it IS a Treeview then say ok
    For other types, it should not allow then. (red sign)
    I hope :)



  • @mrjj
    yes its working fine :)
    i should implement for Pushbutton also .


  • Qt Champions 2017

    @Ratzz
    Super :)



  • @mrjj
    when i click delete button i do ui->label->setText(QString::number(parent->rowCount())); which gives me count of the number of rows. But if i drag the row will be deleted but count is not getting updated. how to update it ?
    Should it be done on GroupDropBox??


  • Qt Champions 2017

    @Ratzz
    GroupBox would not know about parent.
    The Tree should update when the It is removed.

    But then you drag, do you call the buttons on_click so the
    ui->label->setText(QString::number(parent->rowCount()))
    is executed ?
    The groupBox example does not call anything as the Tree removes the item it self so
    I assume that is why you dont see Count Changes.


  • Qt Champions 2017

    Hi
    you could also cheat a bit and use a timer.

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateMinorFrameCount()));
    timer->start(1000);



  • @mrjj
    cheating works fine :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.