change "+" symbol to other symbol when dragging an item from listview to treeview .



  • I have a listview and treeview placed adjecent to each other . I want to drag an item (Item1) from listview and place it to subtree of a treeview . I have implemented to drag item1 and drop it on the subtree. But when i start dragging an item1 from listview shows an "+" when i reach the treeview area . It shows + on the entire area of the treeview . But i want to show + symbol only when it reach the subtree .
    Is it possible to change "+" symbol ? or is it possible to make appear + symbol only at the point it reaches subtree .
    Here is an attachment of how it looks or here.


  • Moderators

    Yes, you can restrict the drop zone to selected parts of the widget.

    In the dragMoveEvent check the cursor position to see if it's above an item and only then call acceptProposedAction() on the event parameter.



  • @Chris-Kawa
    Thanks for the reply .
    can we make the + symbol to change to any other symbol?


  • Lifetime Qt Champion

    Hi,

    There's QDrag::setDragCursor for that purpose



  • @SGaist
    Is this the way to do ?

        QDrag *drag = new QDrag(this);
        QCursor cursor(Qt::OpenHandCursor);
        drag->setDragCursor(cursor.pixmap(), Qt::MoveAction);
    


  • @Chris-Kawa
    We have not used dragMoveEvent instead we used dropMimeData

    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();
                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
            }
    }
    }
    

    So How to do it using dropMimeData


  • Moderators

    We have not used dragMoveEvent instead we used dropMimeData

    These are not alternatives. You can't use one instead of the other. They handle different things at different time.
    dropMimeData is too late. You are interested in specifying if something can be dropped while the item is still dragged, thus you should additionally implement dragMoveEvent.



  • @Chris-Kawa
    I am just checking how to get the cursor position in dragMoveEvent .


  • Moderators

    void YourWidget::dragMoveEvent(QDragMoveEvent * event) {
       QPoint cursorPosition = event->pos();
       //...
    }
    


  • @Chris-Kawa
    I have tried this but doesnot work

    void Window::dragMoveEvent(QDragMoveEvent *event)
    {
        QPoint cursorPosition = event->pos();
        QModelIndex index = ui->listView_Messages->indexAt(cursorPosition);
        if(index.isValid())
        {
            event->acceptProposedAction();
        }
    }
    

    should there be dropEvent as well??


  • Lifetime Qt Champion

    Yes, dragMove is called while you are moving over the target widget, it doesn't replace dropEvent which is for when you drop on your target.



  • @SGaist , @Chris-Kawa
    I am trying to drag an item from listwidget to listview using the following and trying to change to "+" sign but does not work .

    void MainWindow::dragMoveEvent(QDragMoveEvent *event)
    {
        QDrag *drag = new QDrag(this);
         QCursor cursor(Qt::OpenHandCursor);
         drag->setDragCursor(cursor.pixmap(), Qt::MoveAction);
         
        QPoint cursorPosition = event->pos() ;
        QModelIndex index = ui->listView->indexAt(cursorPosition);
        if(index.isValid())
        {
            event->acceptProposedAction();
        }
    }
    
    void MainWindow::dropEvent(QDropEvent *event)
    {
        event->setDropAction(Qt::CopyAction);
        event->accept();
    }
    

    I allows me to copy the items from listwidget to listview . Whst am i missing?


  • Moderators

    You should handle these events on the widget you want to handle the drop (the listview), not on main window. That's one.

    Second - why are you creating new QDrag objects in every move event? There are gonna be tons of them and you are not releasing any of them! There is already a drag in progress. Don't create new ones.

    Don't change the cursor in move event. Set a cursor for drag object where you create it (I'm guessing in mouse press of the treeview?) and don't touch again. It will change when you accept or not a move event.



  • @Chris-Kawa
    How to create the events on widgets? like show below??

    void QListView::dragMoveEvent(QDragMoveEvent *event)
    {
    //
    }
    

    I do not want to to change the cursor on entire drag event from listview to treeview. I just want to change the cursor when there is empty space as shown here and not when like this.


  • Moderators

    Subclass QListView and override dragEnterEvent, dragMoveEvent and dropEvent.

    In dragEnterEvent check the mime type in parameter and event->acceptProposedAction if it's something you can drop onto the list.
    In dragMoveEvent check cursor position and event->acceptProposedAction() or event->ignore(), depending on the cursor position.
    In dropEvent get the event data and physically insert item into list.

    You don't really need to set any cursors. It will change automatically when you accept or ignore the event in the dragMoveEvent.



  • @Chris-Kawa
    I tried this way. Is this the way to do??

    void QlistView::dragMoveEvent(QDragMoveEvent *event) Q_DECL_OVERRIDE
    {
        QPoint cursorPosition = event->pos() ;
        QModelIndex index = BusAnalyzerWindow::ui->listView_Messages->indexAt(cursorPosition);
        if(index.isValid())
            event->acceptProposedAction();
    }
    
    void QListView::dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE
    {
        if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
            event->acceptProposedAction();
    }
    void QListView::dropEvent(QDropEvent *event)
    {
        event->setDropAction(Qt::MoveAction);
        event->accept();
    }
    

    But dragEnterEvent is not member of Qlistview So it failed.


  • Moderators

    You can't just overwrite QListView methods. As I said - subclass QListView and override the methods there.

    Aside from that you should also event->ignore() in a dragMoveEvent else clause.



  • @Chris-Kawa
    I dono how to subclass QListView and override .


  • Moderators

    If you don't know how inheritance works in c++ maybe you should start with some basics before getting into ui development with Qt? You can start here, but I suggest a good book that covers these topics, maybe one of these?



  • @Chris-Kawa
    I will look into it.



  • @Chris-Kawa
    I tried these ways . But none of the two is working.

    myWindow::myWindow(QWidget *parent,QMdiArea *mdiParent):
        QWidget(parent),
        QListView(parent)
        ui(new Ui::myWindow)
    {
        ui->setupUi(this);
    //
    }
    

    nor this as suggested here .

    class myWindow: public QWidget
    {
        Q_OBJECT
     
    public:
        explicit myWindow(QWidget *parent = 0, QMdiArea *mdiParent=0)
            : QListView (paren,mdiParent ){}
          ......
    

    Is there any document giving info about subclass?



  • I used dragMoveEvent to restrict the + symbol.

            void dragMoveEvent(QDragMoveEvent *event)
            {
                QPoint cursorPosition = event->pos() ;
                QModelIndex index = indexAt(cursorPosition);
                if(index.parent().isValid())
                {
                    event->acceptProposedAction();
                }
                else
                    event->ignore();
            }
    

Log in to reply
 

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