PDA

View Full Version : Drag and Drop Crush when second time drag



hnd
23rd February 2019, 11:26
Hi forum again me pls help me.

i just want to do that;

ive got two label and i want to drag first label text to drop second label and code worked fine(i dont know why :))). but i want to do this in second time it crushed.
i think i should to clear mimedata but how?? or what i should to do?

here is my . h file


public:
explicit Dialog(QWidget *parent = nullptr);
~Dialog();
void mousePressEvent(QMouseEvent *event) ;
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
QString labeltext;


private:
Ui::Dialog *ui;

protected:
bool eventFilter(QObject *obj, QEvent *event);


and dialog .cpp


#include <QEvent>
#include <QDragEnterEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QDebug>
#include <QMouseEvent>
#include <QDrag>
#include <QMimeData>

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->label_2->installEventFilter(this);
ui->label_2->setAcceptDrops(true);
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton
&& ui->label->geometry().contains(event->pos()))
{

labeltext = ui->label->text();

mimeData->setText(labeltext);
drag->setMimeData(mimeData);
drag->start();
}
}

bool Dialog::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::DragEnter)
{
QDragEnterEvent *tDragEnterEvent = static_cast<QDragEnterEvent *>(event);
tDragEnterEvent->acceptProposedAction();

return true;
}

else if (event->type() == QEvent::DragMove)
{
QDragMoveEvent *tDragMoveEvent = static_cast<QDragMoveEvent *>(event);
tDragMoveEvent->acceptProposedAction();

return true;
}


else if (event->type() == QEvent::Drop)
{
QDropEvent *tDropEvent = static_cast<QDropEvent *>(event);
tDropEvent->acceptProposedAction();

ui->label_2->setText(mimeData->text());


return true;
}

return QObject::eventFilter(obj, event);
}

please help mee pleeeeaseeeee :((

(and pls i dont want to use subclass for this)

anda_skoa
23rd February 2019, 12:28
A crash is usually a good source to find what is wrong, e.g. by running the program in debugger and checking which call causes the crash.
Especially if you have a reliable way of triggering the crash.

Educated guess: the second time calls methods on a now invalid QDrag pointer.

Cheers,
_

hnd
24th February 2019, 18:03
All my respect sir,

What am i doing please show me the way

anda_skoa
24th February 2019, 22:04
1) Compile the program in debug mode
2) Run the program in the debugger
3) Check where it crashes

Debugging a crash is the most fundamental thing to learn.

Telling you the cause and/or solution for this specific crash does not help you in the long run

Cheers,
_

d_stranz
26th February 2019, 00:32
Educated guess: the second time calls methods on a now invalid QDrag pointer.

And probably an invalid QMimeData pointer as well, since QDrag::setMimeData() takes ownership and if the QDrag instance is destroyed, so will be the QMimeData instance.


drag->start();

I can't find a QDrag:: start() method, so where did this come from, and how did you get the code to compile in the first place?

anda_skoa
26th February 2019, 07:22
And probably an invalid QMimeData pointer as well, since QDrag::setMimeData() takes ownership and if the QDrag instance is destroyed, so will be the QMimeData instance.

Yep



I can't find a QDrag:: start() method, so where did this come from, and how did you get the code to compile in the first place?

Deprecated and thus not part of the API documentation.
But still in the header.

Cheers,
_