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

[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.2k Views
  • 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.
  • M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 10 Oct 2015, 07:31 last edited by mrjj 10 Oct 2015, 07:41
    #2

    Hi
    Yeah that should be possible
    if you set setAcceptDrops(true);
    for your mainwind/dialog
    and then handle
    void Window::dragMoveEvent(QDragMoveEvent *event)
    and
    void Window::dropEvent(QDropEvent *event)

    I think if you set the action to move, the Tree will remove it when
    drop triggers but not sure.
    Else you just need to do when you do when button is clicked.

    Look here for info about drag/drop
    http://www.informit.com/articles/article.aspx?p=1405546

    Note: Entire window will be drop target so in DragMove event they seems to do
    if (event->answerRect().intersects(dropFrame->geometry()))
    to check if it was over the wanted area. In this case a frame but could be button.

    1 Reply Last reply
    1
    • R Offline
      R Offline
      Ratzz
      wrote on 10 Oct 2015, 07:54 last edited by Ratzz 10 Oct 2015, 07:55
      #3

      @mrjj
      Thanks for the reply .
      I have set setAcceptDrops(true); ui->treeView->setAcceptDrops(true);
      and created void Window::dragMoveEvent(QDragMoveEvent *event)
      and
      void Window::dropEvent(QDropEvent *event)

      void Window::dragMoveEvent(QDragMoveEvent *event)
      {
          Window *source = qobject_cast<Window *>(event->source());
          if (source && source != this) {
              event->setDropAction(Qt::MoveAction);
              event->accept();
          }
      }
      void Window::dropEvent(QDropEvent *event)
      {
          Window *source =qobject_cast<Window *>(event->source());
          if (source && source != this)
          {
              event->setDropAction(Qt::MoveAction);
              event->accept();
          }
      }
      

      I am unaware of how to set condition .

      --Alles ist gut.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 10 Oct 2015, 08:21 last edited by
        #4

        @Ratzz said:

        Hmm. Maybe I misunderstood you,

        You want to drag from the tree to outside. ?
        so ui->treeView->setAcceptDrops(true);
        tells treeview it can be drop target. ( dropped on)
        Should also tell you MainWindow
        so Window (which is your mainwindow ? )
        Normally it would be named MainWindow but if Window is you mainwin then all fine.

        "set condition " ?
        you mean
        if (event->answerRect().intersects(dropFrame->geometry()))
        or ?

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Ratzz
          wrote on 10 Oct 2015, 08:24 last edited by
          #5

          @mrjj
          Yes , I want to drag the item from the tree to outside and to pushbutton "Delete item".
          I have to set setAcceptDrops(true); to pushbutton and groupbox area right?
          yes i meant if (event->answerRect().intersects(dropFrame->geometry()))

          --Alles ist gut.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 10 Oct 2015, 08:53 last edited by mrjj 10 Oct 2015, 08:54
            #6

            Ok so your mainwindow is the drop target
            and you should call etAcceptDrops(true); in constructor.

            and this line
            if (event->answerRect().intersects(dropFrame->geometry()))

            should be used to check its actually dropped on the button.
            dropFrame should just be the name of your delete button.
            It says if the Area for the drop is within the dropFrame Area.
            as it , "dropped on the frame"

            sosomething like

            void (Main?)Window::dropEvent(QDropEvent *event)
            {
                Window *source =qobject_cast<Window *>(event->source());
                if (source && source != this &&  
                 (event->answerRect().intersects(ui->ThatButton->geometry())))
                {
                    event->setDropAction(Qt::MoveAction);
                    event->accept();
                }
            

            All "Window" should be "MainWindow" in case its normal project. Unless you did
            rename name it to Window yourself :)

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Ratzz
              wrote on 10 Oct 2015, 09:02 last edited by
              #7

              @mrjj
              Is the answerRect() member of QDropevnt(); ?
              Because i get error

               error: 'class QDropEvent' has no member named 'answerRect'
              (event->answerRect().intersects(ui->pushButton_DeleteMinorFrame->geometry())))
                       ^

              --Alles ist gut.

              M 1 Reply Last reply 10 Oct 2015, 16:36
              0
              • R Ratzz
                10 Oct 2015, 09:02

                @mrjj
                Is the answerRect() member of QDropevnt(); ?
                Because i get error

                 error: 'class QDropEvent' has no member named 'answerRect'
                (event->answerRect().intersects(ui->pushButton_DeleteMinorFrame->geometry())))
                         ^
                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 10 Oct 2015, 16:36 last edited by
                #8

                @Ratzz said:

                event->answerRect()

                Hi. sorry my bad. its part of QDragMoveEvent
                so should check in ::dragMoveEvent instead.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 10 Oct 2015, 20:14 last edited by
                  #9

                  Hi,

                  Wouldn't it be simpler to subclass QPushButton and add the drag'n'drop logic directly in it ?

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

                  M R 2 Replies Last reply 10 Oct 2015, 20:27
                  0
                  • S SGaist
                    10 Oct 2015, 20:14

                    Hi,

                    Wouldn't it be simpler to subclass QPushButton and add the drag'n'drop logic directly in it ?

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 10 Oct 2015, 20:27 last edited by
                    #10

                    @SGaist
                    Yeah that is also an option , but since normally you already have a QMainWindow
                    subclass, i thought it could be slightly faster to add
                    and would work with most widgets after.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Ratzz
                      wrote on 12 Oct 2015, 04:24 last edited by Ratzz 10 Dec 2015, 05:29
                      #11

                      @mrjj
                      Adding event dragMoveEvent will work ??
                      Should not i add QDropevnt() ??

                      void MyWindow::dragMoveEvent(QDragMoveEvent *event)
                      {
                            MyWindow *source =qobject_cast<MyWindow *>(event->source());
                          if (source && source != this &&
                           (event->answerRect().intersects(ui->pushButton_Delete->geometry())))
                          {
                              event->setDropAction(Qt::MoveAction);
                              event->accept();
                          }
                      }
                      

                      I added the above but did not .

                      --Alles ist gut.

                      1 Reply Last reply
                      0
                      • S SGaist
                        10 Oct 2015, 20:14

                        Hi,

                        Wouldn't it be simpler to subclass QPushButton and add the drag'n'drop logic directly in it ?

                        R Offline
                        R Offline
                        Ratzz
                        wrote on 12 Oct 2015, 04:55 last edited by
                        #12

                        @SGaist
                        How to subclass QPushButton and add the drag'n'drop logic directly in it ??

                        --Alles ist gut.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 12 Oct 2015, 22:25 last edited by
                          #13

                          The same you would any other widget. Take a look at the Drag And Drop chapter in Qt's documentation

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

                          R 1 Reply Last reply 13 Oct 2015, 08:24
                          0
                          • S SGaist
                            12 Oct 2015, 22:25

                            The same you would any other widget. Take a look at the Drag And Drop chapter in Qt's documentation

                            R Offline
                            R Offline
                            Ratzz
                            wrote on 13 Oct 2015, 08:24 last edited by
                            #14

                            @SGaist
                            As suggested by @mrjj should i add QDropevnt()??
                            how will the dragMoveEvent come to know that i have to delete ?? Just dragging to pushButton_Delete will work??
                            When to call slot of deleting items??

                            --Alles ist gut.

                            M 1 Reply Last reply 13 Oct 2015, 09:07
                            0
                            • R Ratzz
                              13 Oct 2015, 08:24

                              @SGaist
                              As suggested by @mrjj should i add QDropevnt()??
                              how will the dragMoveEvent come to know that i have to delete ?? Just dragging to pushButton_Delete will work??
                              When to call slot of deleting items??

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 13 Oct 2015, 09:07 last edited by
                              #15

                              @Ratzz
                              Hi yes you would still need the DropEvent.

                              This works with ListWidget with dragDrop mode set to DragOnly.
                              Since the DropAction is "move", it did remove the item from the list.
                              Your result may vary.

                              #ifndef DROPBUTTON_H
                              #define DROPBUTTON_H
                              
                              #include <QPushButton>
                              #include <QDragMoveEvent>
                              
                              class DropButton : public QPushButton {
                                Q_OBJECT
                               public:
                                explicit  DropButton(QWidget* parent = 0) : QPushButton(parent) {
                                  setAcceptDrops(true);
                                }
                               protected:
                                void dragEnterEvent(QDragEnterEvent* event) {
                                  event->acceptProposedAction();
                                }
                              
                                void dropEvent(QDropEvent* event) {
                                  event->setDropAction(Qt::MoveAction);
                                  event->accept();
                                }
                              };
                              
                              #endif // DROPBUTTON_H
                              
                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                Ratzz
                                wrote on 13 Oct 2015, 09:55 last edited by Ratzz
                                #16

                                @mrjj
                                I tried this

                                #include <QDragMoveEvent>
                                #include <QDropEvent>
                                protected:
                                    void dragMoveEvent(QDragMoveEvent *event);
                                    void dropEvent(QDropEvent *event);
                                
                                ui->pushButton_DeleteMinorFrame->setAcceptDrops(true);
                                
                                void Window::dragMoveEvent(QDragMoveEvent *event)
                                {
                                    Window *source =qobject_cast<Window *>(event->source());
                                    if (source && source != this &&
                                            (event->answerRect().intersects(ui->pushButton_DeleteMinorFrame->geometry())))
                                    {
                                        event->acceptProposedAction();
                                    }
                                }
                                
                                void Window::dropEvent(QDropEvent *event)
                                {
                                    event->setDropAction(Qt::MoveAction);
                                    event->accept();
                                }
                                

                                But above does not work for me. Where did i miss??
                                Control does not come to these function dragMoveEvent/dropEvent when i drag and drop

                                --Alles ist gut.

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 13 Oct 2015, 10:34 last edited by mrjj
                                  #17

                                  Hi you have to use the Dropbutton also.

                                  That is why I though using Mainwindow would be easier.

                                  Best way to do that is to use the promote feature.

                                  1. save the code in a file called dropbutton.h
                                    (just to be safe, also create dropbutton.cpp as empty)
                                  2. right click on your button you want to be a drop button.
                                    Select "Promote To..."
                                    In "Promoted classname" type
                                    DropButton
                                    (case Matters!)
                                    in
                                    header file : type
                                    dropbutton.h
                                    Then click "Add" and then Promote.
                                    Now when you run the program, the normal buttons becomes the DropButton.
                                  1 Reply Last reply
                                  0
                                  • C Offline
                                    C Offline
                                    ClapCreative
                                    wrote on 13 Oct 2015, 10:44 last edited by
                                    #18

                                    Thanks mates

                                    Try us out ClapCreative.com is a professional SEO company Los Angeles offering quality SEO&#x2F;PPC Services. Contact us info@clapcreative.com or +13238632896

                                    1 Reply Last reply
                                    0
                                    • R Offline
                                      R Offline
                                      Ratzz
                                      wrote on 13 Oct 2015, 10:58 last edited by
                                      #19

                                      @mrjj
                                      Yes, i Tried you code which turns out to be a Dropbutton. How to implement it in my code ?

                                      --Alles ist gut.

                                      M 1 Reply Last reply 13 Oct 2015, 11:00
                                      0
                                      • R Ratzz
                                        13 Oct 2015, 10:58

                                        @mrjj
                                        Yes, i Tried you code which turns out to be a Dropbutton. How to implement it in my code ?

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 13 Oct 2015, 11:00 last edited by
                                        #20

                                        @Ratzz
                                        As I wrote in answer ?
                                        Using the promote feature.
                                        Which will runtime replace a normal button with the DropButton.
                                        Or you can new a button you self
                                        Like
                                        Dropbutton *but=Dropbutton (this)
                                        and insert into your window/layout.

                                        R 1 Reply Last reply 13 Oct 2015, 11:25
                                        0
                                        • M mrjj
                                          13 Oct 2015, 11:00

                                          @Ratzz
                                          As I wrote in answer ?
                                          Using the promote feature.
                                          Which will runtime replace a normal button with the DropButton.
                                          Or you can new a button you self
                                          Like
                                          Dropbutton *but=Dropbutton (this)
                                          and insert into your window/layout.

                                          R Offline
                                          R Offline
                                          Ratzz
                                          wrote on 13 Oct 2015, 11:25 last edited by
                                          #21

                                          @mrjj
                                          Yes its working now fine..
                                          I have group box adjacent to treeview. Can i implement the same to groupbox?

                                          --Alles ist gut.

                                          M 1 Reply Last reply 13 Oct 2015, 11:28
                                          0

                                          11/54

                                          12 Oct 2015, 04:24

                                          43 unread
                                          • Login

                                          • Login or register to search.
                                          11 out of 54
                                          • First post
                                            11/54
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved