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. change "+" symbol to other symbol when dragging an item from listview to treeview .
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
treeviewlistview
22 Posts 3 Posters 11.0k 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.
  • RatzzR Offline
    RatzzR Offline
    Ratzz
    wrote on last edited by Ratzz
    #3

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

    --Alles ist gut.

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

      Hi,

      There's QDrag::setDragCursor for that purpose

      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

        Hi,

        There's QDrag::setDragCursor for that purpose

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

        @SGaist
        Is this the way to do ?

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

        --Alles ist gut.

        1 Reply Last reply
        0
        • Chris KawaC Chris Kawa

          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.

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

          @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

          --Alles ist gut.

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #7

            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.

            RatzzR 1 Reply Last reply
            0
            • Chris KawaC Chris Kawa

              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.

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

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

              --Alles ist gut.

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #9
                void YourWidget::dragMoveEvent(QDragMoveEvent * event) {
                   QPoint cursorPosition = event->pos();
                   //...
                }
                
                RatzzR 1 Reply Last reply
                0
                • Chris KawaC Chris Kawa
                  void YourWidget::dragMoveEvent(QDragMoveEvent * event) {
                     QPoint cursorPosition = event->pos();
                     //...
                  }
                  
                  RatzzR Offline
                  RatzzR Offline
                  Ratzz
                  wrote on last edited by
                  #10

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

                  --Alles ist gut.

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

                    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.

                    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

                      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.

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

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

                      --Alles ist gut.

                      1 Reply Last reply
                      0
                      • Chris KawaC Offline
                        Chris KawaC Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on last edited by Chris Kawa
                        #13

                        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.

                        RatzzR 1 Reply Last reply
                        0
                        • Chris KawaC Chris Kawa

                          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.

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

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

                          --Alles ist gut.

                          1 Reply Last reply
                          0
                          • Chris KawaC Offline
                            Chris KawaC Offline
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            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.

                            RatzzR 1 Reply Last reply
                            0
                            • Chris KawaC Chris Kawa

                              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.

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

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

                              --Alles ist gut.

                              1 Reply Last reply
                              0
                              • Chris KawaC Offline
                                Chris KawaC Offline
                                Chris Kawa
                                Lifetime Qt Champion
                                wrote on last edited by
                                #17

                                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.

                                RatzzR 1 Reply Last reply
                                0
                                • Chris KawaC Chris Kawa

                                  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.

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

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

                                  --Alles ist gut.

                                  1 Reply Last reply
                                  0
                                  • Chris KawaC Offline
                                    Chris KawaC Offline
                                    Chris Kawa
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #19

                                    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?

                                    RatzzR 1 Reply Last reply
                                    0
                                    • Chris KawaC Chris Kawa

                                      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?

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

                                      @Chris-Kawa
                                      I will look into it.

                                      --Alles ist gut.

                                      RatzzR 1 Reply Last reply
                                      0
                                      • RatzzR Ratzz

                                        @Chris-Kawa
                                        I will look into it.

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

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

                                        --Alles ist gut.

                                        RatzzR 1 Reply Last reply
                                        0
                                        • RatzzR Ratzz

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

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

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

                                          --Alles ist gut.

                                          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