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();
private:
Ui::Dialog *ui;
protected:
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);
To copy to clipboard, switch view to plain text mode
and dialog .cpp
#include <QEvent>
#include <QDragEnterEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QDebug>
#include <QMouseEvent>
#include <QDrag>
#include <QMimeData>
ui(new Ui::Dialog)
{
ui->setupUi(this);
ui->label_2->installEventFilter(this);
ui->label_2->setAcceptDrops(true);
}
Dialog::~Dialog()
{
delete ui;
}
{
if (event->button() == Qt::LeftButton
&& ui->label->geometry().contains(event->pos()))
{
labeltext = ui->label->text();
mimeData->setText(labeltext);
drag->setMimeData(mimeData);
drag->start();
}
}
{
if (event
->type
() == QEvent::DragEnter) {
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
);
}
#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);
}
To copy to clipboard, switch view to plain text mode
please help mee pleeeeaseeeee :((
(and pls i dont want to use subclass for this)
Bookmarks