Retrieve parent through a Drag & Drop from a QtreeWidget
-
Hello guys,
I would like to set up a drag & drop in my qtreewidget and get the value of the parent where I moved my item.
To enable drag & drop, I simply enabled drap and drop in qt designer.
But I have no idea how to retrieve the parent information when I move my item. I can't find a signal corresponding to drag & drop in the QtreeWidget documentation.
Beginner on Qt do you have a simple solution to answer my problem ?
Thanks a lot for your help ! =)
-
@mrjj ah okay thank you I understand a bit better.
Hmmmm and what about the dropEvent function what should i change to retrieve the parent information ?
Hi
Super. we need to create our own class (subclass) to change the default drop handlers as they are virtual function and
the is the c++ way to implement/change behavior.Hmmmm and what about the dropEvent function what should i change to retrieve the parent information ?
Well Im not 100% sure that the default MimeData contains both row and col.
The standard mimeformat used can be read like this
so it might be enough.
If not, you need to handle the DragStart function also to include the information you need.
-
Hi
If you want better control you will subclass and override (make) the Drop functions and in there you can
get info for the QTreeWidget etc.https://doc.qt.io/qt-5/dnd.html
I imagine its the QDragMoveEvent you want
so when user drag over some parent, you can read its value.If first after user drop the item, then the QDropEvent
Can I ask what you need the parent value for ?
-
@mrjj said in Retrieve parent through a Drag & Drop from a QtreeWidget:
I imagine its the QDragMoveEvent you want
so when user drag over some parent, you can read its value.
If first after user drop the item, then the QDropEvent
Can I ask what you need the parent value for ?Sorry if I wasn't clear.
Actually I want to retrieve the parent once my item has been dropped.(My QtreeWidget is linked with a database, and i need the parent value to complete it because my database does the hierarchization of my tree)
So i should use the QDropEvent right for my case ?
-
As suggested i am trying to override the QDropEvent function.
So i wrote in my .h file this :
void dropEvent(QDropEvent *event) override;
and i saw in the documentation that data are stored in a QMime so i have wrote this code to check if i can have the Parent information
void MainWindow::dropEvent(QDropEvent *event) { const QMimeData *mimeData = event->mimeData(); qDebug()<<mimeData->text(); }
But there is nothing displayed. I'm stuck :(, do you have any other ideas?
Thanks -
Did you set the mime data in your QDrag object like in the Drag and Drop example that mrjj linked to above?
-
@mchinand Hmmm what do you mean ?
I have set the mime data like this in my function :void MainWindow::dropEvent(QDropEvent *event) { const QMimeData *mimeData = new QMimeData; mimeData = event->mimeData(); qDebug()<<mimeData->text(); }
@coilo What is your object wanting to get the parent of the dragged QTreeWidget? Maybe there is another solution. I point this out since by default the dnd between QAbstractItemView (like the QTreeWidget) only sends data encoded in a special format and in that data there is no relationship or information to obtain the parent
-
As suggested i am trying to override the QDropEvent function.
So i wrote in my .h file this :
void dropEvent(QDropEvent *event) override;
and i saw in the documentation that data are stored in a QMime so i have wrote this code to check if i can have the Parent information
void MainWindow::dropEvent(QDropEvent *event) { const QMimeData *mimeData = event->mimeData(); qDebug()<<mimeData->text(); }
But there is nothing displayed. I'm stuck :(, do you have any other ideas?
ThanksHi,
@coilo said in Retrieve parent through a Drag & Drop from a QtreeWidget:
As suggested i am trying to override the QDropEvent function.
So i wrote in my .h file this :
void dropEvent(QDropEvent *event) override;
and i saw in the documentation that data are stored in a QMime so i have wrote this code to check if i can have the Parent information
void MainWindow::dropEvent(QDropEvent *event) { const QMimeData *mimeData = event->mimeData(); qDebug()<<mimeData->text(); }
But there is nothing displayed. I'm stuck :(, do you have any other ideas?
ThanksUnless MainWindow is your QTreeWidget it won't get called.
You need to subclass QTreeWidget if you want to alter its DND behavior.
-
@coilo What is your object wanting to get the parent of the dragged QTreeWidget? Maybe there is another solution. I point this out since by default the dnd between QAbstractItemView (like the QTreeWidget) only sends data encoded in a special format and in that data there is no relationship or information to obtain the parent
@eyllanesc Hmmm i see, so there is no way to retrieve the parent's information once the item has been dropped ?
-
Hi,
@coilo said in Retrieve parent through a Drag & Drop from a QtreeWidget:
As suggested i am trying to override the QDropEvent function.
So i wrote in my .h file this :
void dropEvent(QDropEvent *event) override;
and i saw in the documentation that data are stored in a QMime so i have wrote this code to check if i can have the Parent information
void MainWindow::dropEvent(QDropEvent *event) { const QMimeData *mimeData = event->mimeData(); qDebug()<<mimeData->text(); }
But there is nothing displayed. I'm stuck :(, do you have any other ideas?
ThanksUnless MainWindow is your QTreeWidget it won't get called.
You need to subclass QTreeWidget if you want to alter its DND behavior.
@SGaist said in Retrieve parent through a Drag & Drop from a QtreeWidget:
Unless MainWindow is your QTreeWidget it won't get called.
Hmmm yes my MainWindow is not my QTreeWidget.
What do you mean by subclassing my QTreeWidget ? What should i change ? -
@SGaist said in Retrieve parent through a Drag & Drop from a QtreeWidget:
Unless MainWindow is your QTreeWidget it won't get called.
Hmmm yes my MainWindow is not my QTreeWidget.
What do you mean by subclassing my QTreeWidget ? What should i change ?Hi
Subclassing means make a new class based on the mentioned classclass MyTreeWidget : public QTreeWidget { public: explicit MyTreeWidget(QWidget* parent = nullptr) : QTreeWidget(parent) {} };
so you want to add the dropXXX functions to that.
and then use MyTreeWidget instead of the plain QTreeWidget
-
Hi
Subclassing means make a new class based on the mentioned classclass MyTreeWidget : public QTreeWidget { public: explicit MyTreeWidget(QWidget* parent = nullptr) : QTreeWidget(parent) {} };
so you want to add the dropXXX functions to that.
and then use MyTreeWidget instead of the plain QTreeWidget
-
@mrjj ah okay thank you I understand a bit better.
Hmmmm and what about the dropEvent function what should i change to retrieve the parent information ?
Hi
Super. we need to create our own class (subclass) to change the default drop handlers as they are virtual function and
the is the c++ way to implement/change behavior.Hmmmm and what about the dropEvent function what should i change to retrieve the parent information ?
Well Im not 100% sure that the default MimeData contains both row and col.
The standard mimeformat used can be read like this
so it might be enough.
If not, you need to handle the DragStart function also to include the information you need.