#include "dragwidget.h"
#include "draglabel.h"
#include "dragicon.h"
DragWidget::DragWidget(QWidget *parent)
: QFrame(parent)
{
setMinimumSize(250,400);
setAcceptDrops(true);
setFrameStyle(QFrame::StyledPanel|QFrame::Raised);
DragIcon *icon_one = new DragIcon(QPixmap(":/images/sheep.png"),this);
DragIcon *icon_two = new DragIcon(QPixmap(":/images/heart.png"),this);
DragIcon *icon_three = new DragIcon(QPixmap(":/images/fish.png"),this);
icon_one->resize(50, 50);
icon_one->move(50,20);
icon_two->resize(50, 50);
icon_two->move(150,20);
icon_three->resize(50, 50);
icon_three->move(260,20);
}
DragWidget::~DragWidget()
{
}
void DragWidget::dragEnterEvent(QDragEnterEvent *e)
{
if (e->mimeData()->hasText() || e->mimeData()->hasFormat("Drag-Icon"))
{
if(children().contains(e->source()))
{
e->setDropAction(Qt::MoveAction);
e->accept();
}
else
e->acceptProposedAction();
}
}
void DragWidget::dragMoveEvent(QDragMoveEvent *e)
{
if (e->mimeData()->hasText() || e->mimeData()->hasFormat("Drag-Icon"))
{
if(children().contains(e->source()))
{
e->setDropAction(Qt::MoveAction);
e->accept();
}
else
e->acceptProposedAction();
}
}
void DragWidget::dropEvent(QDropEvent *e)
{
if (e->mimeData()->hasFormat("Drag-Icon")) {
QByteArray data = e->mimeData()->data("Drag-Icon");
QDataStream stream(&data,QIODevice::ReadOnly);
QPixmap pix;
QPoint offset;
stream >> pix >> offset;
DragIcon *icon = new DragIcon(pix,this);
icon->move(e->pos() - offset);
icon->resize(50, 50);
icon->show();
if (children().contains(e->source())) {
e->setDropAction(Qt::MoveAction);
e->accept();
}
else e->acceptProposedAction();
}
else if (e->mimeData()->hasFormat("Drag-Text"))
{
QByteArray data = e->mimeData()->data("Drag-Text");
QDataStream stream(&data,QIODevice::ReadOnly);
QString text;
QPoint offset;
stream >> text >> offset;
DragLabel *label = new DragLabel(text,this);
label->move(e->pos() - offset);
label->show();
if (children().contains(e->source())) {
e->setDropAction(Qt::MoveAction);
e->accept();
}
else e->acceptProposedAction();
}
else if (e->mimeData()->hasText())
{
QStringList strList = e->mimeData()->text().split(QRegExp("\\s+"),QString::SkipEmptyParts);
QPoint pos = e->pos();
foreach(QString str, strList) {
DragLabel *dragLabel = new DragLabel(str,this);
dragLabel->move(pos);
dragLabel->show();
pos += QPoint(dragLabel->width(),0);
}
if (children().contains(e->source())) {
e->setDropAction(Qt::MoveAction);
e->accept();
}
else {
e->acceptProposedAction();
}
}
else {
e->ignore();
}
}
void DragWidget::slotChangeStyle(QString style)
{
QApplication::setStyle(QStyleFactory::create(style));
QApplication::setPalette(QApplication::style()->standardPalette());
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页