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.
  • 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
                        • R Ratzz
                          13 Oct 2015, 11:25

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

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

                          @Ratzz
                          Super.
                          Well If you promote the GroupBox, there might be issue with
                          Parent. I have never tried it.
                          Try it :)
                          If it says funny stuff , just demote it to a groupbox again.

                          Else its pretty much the same, except new filename and new name.

                          #include <QGroupBox>
                          #include <QDragMoveEvent>
                          
                          class DropGroupBox : public QGroupBox {
                            Q_OBJECT
                           public:
                            explicit  DropGroupBox(QWidget* parent = 0) : QGroupBox(parent) {
                              setAcceptDrops(true);
                            }
                           protected:
                            void dragEnterEvent(QDragEnterEvent* event) {
                              event->acceptProposedAction();
                            }
                          
                            void dropEvent(QDropEvent* event) {
                              event->setDropAction(Qt::MoveAction);
                              event->accept();
                            }
                          };
                          
                          R 1 Reply Last reply 13 Oct 2015, 12:04
                          0
                          • M mrjj
                            13 Oct 2015, 11:28

                            @Ratzz
                            Super.
                            Well If you promote the GroupBox, there might be issue with
                            Parent. I have never tried it.
                            Try it :)
                            If it says funny stuff , just demote it to a groupbox again.

                            Else its pretty much the same, except new filename and new name.

                            #include <QGroupBox>
                            #include <QDragMoveEvent>
                            
                            class DropGroupBox : public QGroupBox {
                              Q_OBJECT
                             public:
                              explicit  DropGroupBox(QWidget* parent = 0) : QGroupBox(parent) {
                                setAcceptDrops(true);
                              }
                             protected:
                              void dragEnterEvent(QDragEnterEvent* event) {
                                event->acceptProposedAction();
                              }
                            
                              void dropEvent(QDropEvent* event) {
                                event->setDropAction(Qt::MoveAction);
                                event->accept();
                              }
                            };
                            
                            R Offline
                            R Offline
                            Ratzz
                            wrote on 13 Oct 2015, 12:04 last edited by Ratzz
                            #23

                            @mrjj
                            i promoted the group using Qpushbutton but not allow me to promote the GroupBox. so i added dropgroupBox.h and promoted it.
                            But its not working.

                            --Alles ist gut.

                            M 1 Reply Last reply 13 Oct 2015, 14:37
                            0
                            • R Ratzz
                              13 Oct 2015, 12:04

                              @mrjj
                              i promoted the group using Qpushbutton but not allow me to promote the GroupBox. so i added dropgroupBox.h and promoted it.
                              But its not working.

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 13 Oct 2015, 14:37 last edited by
                              #24

                              @Ratzz
                              Hi
                              Something must gone wrong. maybe.
                              Tried it here and it does accept drops

                              https://www.dropbox.com/s/7bkqatzm6vt1kmz/promotedgb.zip?dl=0

                              R 1 Reply Last reply 14 Oct 2015, 04:51
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 13 Oct 2015, 18:42 last edited by
                                #25

                                Out of curiosity… Why are you creating so many widgets that should allow to delete items from your view using dnd ? It doesn't feel very intuitive nor following the guidelines of major OSs

                                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 14 Oct 2015, 04:58
                                0
                                • M mrjj
                                  13 Oct 2015, 14:37

                                  @Ratzz
                                  Hi
                                  Something must gone wrong. maybe.
                                  Tried it here and it does accept drops

                                  https://www.dropbox.com/s/7bkqatzm6vt1kmz/promotedgb.zip?dl=0

                                  R Offline
                                  R Offline
                                  Ratzz
                                  wrote on 14 Oct 2015, 04:51 last edited by Ratzz
                                  #26

                                  @mrjj
                                  Yes your code is working . But do not know in my case its not working .
                                  You have used Listwidget and i have used list view . Does it matters??

                                  --Alles ist gut.

                                  1 Reply Last reply
                                  0
                                  • S SGaist
                                    13 Oct 2015, 18:42

                                    Out of curiosity… Why are you creating so many widgets that should allow to delete items from your view using dnd ? It doesn't feel very intuitive nor following the guidelines of major OSs

                                    R Offline
                                    R Offline
                                    Ratzz
                                    wrote on 14 Oct 2015, 04:58 last edited by
                                    #27

                                    @SGaist
                                    Yes , My treeview is in between listview and groupbox . even if the listview items are dragged to the pushbutton_delete its getting deleted. which in my case should not. what is the best way to do ??

                                    --Alles ist gut.

                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on 14 Oct 2015, 21:36 last edited by
                                      #28

                                      Implement it in one of the usual ways:

                                      • React to the delete or backspace key
                                      • Have a delete button that will delete the currently selected row/column etc.
                                      • Have a dedicated column with e.g. a QPushButton to delete the row completely.

                                      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 15 Oct 2015, 06:39
                                      0
                                      • S SGaist
                                        14 Oct 2015, 21:36

                                        Implement it in one of the usual ways:

                                        • React to the delete or backspace key
                                        • Have a delete button that will delete the currently selected row/column etc.
                                        • Have a dedicated column with e.g. a QPushButton to delete the row completely.
                                        R Offline
                                        R Offline
                                        Ratzz
                                        wrote on 15 Oct 2015, 06:39 last edited by
                                        #29

                                        @SGaist
                                        I already have a delete button which deletes entire row.

                                        --Alles ist gut.

                                        R 1 Reply Last reply 15 Oct 2015, 10:26
                                        0
                                        • R Ratzz
                                          15 Oct 2015, 06:39

                                          @SGaist
                                          I already have a delete button which deletes entire row.

                                          R Offline
                                          R Offline
                                          Ratzz
                                          wrote on 15 Oct 2015, 10:26 last edited by
                                          #30

                                          @mrjj
                                          The code you shared is working (group box).

                                          --Alles ist gut.

                                          M 1 Reply Last reply 15 Oct 2015, 10:42
                                          0

                                          20/54

                                          13 Oct 2015, 11:00

                                          • Login

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