[SOLVED]How to delete an item from the treeview by droping an item to pushbutton or to an group box
-
@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 -
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.
- save the code in a file called dropbutton.h
(just to be safe, also create dropbutton.cpp as empty) - 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.
- save the code in a file called dropbutton.h
-
Thanks mates
-
@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. -
@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(); } };
-
@Ratzz
Hi
Something must gone wrong. maybe.
Tried it here and it does accept dropshttps://www.dropbox.com/s/7bkqatzm6vt1kmz/promotedgb.zip?dl=0
-
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
-
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.
-
@Ratzz
Hmm maybe you can check with
sender();
in dragEnterEvent
and if its the not a Treeview then reject the droplike
QTreeView* theone= dynamic_cast<QTreeView*>(sender()); if( theone!= NULL ) { event->acceptProposedAction(); }