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