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