一、效果图
二、代码构造思路
捕获鼠标点击事件,构建QDrag对象设置部件pixmap.重写dropEvent dragEnterEvent 实现自定义控件拖拽
三、代码片段
#include "dragdropwidget.h"
#include "ui_dragdropwidget.h"
dragdropwidget::dragdropwidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::dragdropwidget)
{
m_srcIndex = -1;
setAcceptDrops(true);
ui->setupUi(this);
for(int i=0;i<4;i++){
QLabel *btn = new QLabel(this);
btn->setStyleSheet("background-color:#A9A9A9;font-family: Microsoft YaHei;font:bold 12px;color: #666666;text-align:right;");
btn->setText("LeftBtn"+QString::number(i));
ui->verticalLayout->addWidget(btn);
}
ui->verticalLayout->addStretch();
for(int i=0;i<4;i++){
QLabel *btn = new QLabel(this);
btn->setStyleSheet("background-color:#A9A9A9;font-family: Microsoft YaHei;font:bold 12px;color: #666666;text-align:center;");
btn->setText("RightBtn"+QString::number(i));
ui->verticalLayout_2->addWidget(btn);
}
ui->verticalLayout_2->addStretch();
m_bleft = false;
m_bright = false;;
}
dragdropwidget::~dragdropwidget()
{
delete ui;
}
int dragdropwidget::itemPressed(const QPoint &pos)
{
for (int index = 0; index < ui->verticalLayout->count()-1; index++)
{
QLayoutItem *item = ui->verticalLayout->itemAt(index);
if (item->widget()->isVisible())
{
QRect rect = item->widget()->geometry();
if(rect.contains(pos)){
m_bleft = true;
return index;
}
}
}
for (int index = 0; index < ui->verticalLayout_2->count()-1; index++)
{
QLayoutItem *item = ui->verticalLayout_2->itemAt(index);
if (item->widget()->isVisible())
{
QRect rect = item->widget()->geometry();
if(rect.contains(pos)){
m_bright = true;
return index;
}
}
}
return -1;
}
void dragdropwidget::dropEvent(QDropEvent *event)
{
qDebug()<<"dropEvent";
if(m_bleft)
{
ui->verticalLayout_2->insertWidget(ui->verticalLayout_2->count()-1,m_pwidget);m_pwidget->show();
}
else if(m_bright)
{
ui->verticalLayout->insertWidget(ui->verticalLayout->count()-1,m_pwidget);m_pwidget->show();
}
event->setDropAction(Qt::MoveAction);
event->accept();
}
void dragdropwidget::dragEnterEvent(QDragEnterEvent *event)
{
event->setDropAction(Qt::MoveAction);
event->accept();
}
void dragdropwidget::mousePressEvent(QMouseEvent *event)
{
m_bleft = false;
m_bright = false;
m_srcIndex = itemPressed(event->pos());
qDebug()<<"m_srcIndex"<<m_srcIndex;
if(m_srcIndex == -1)
return;
QWidget *child;
if(m_bleft)
{
child = ui->verticalLayout->takeAt(m_srcIndex)->widget();
ui->verticalLayout->removeWidget(child);
}
else if(m_bright)
{
child = ui->verticalLayout_2->takeAt(m_srcIndex)->widget();
ui->verticalLayout_2->removeWidget(child);
}
child->hide();
m_pwidget = child;
ui->verticalLayout->removeWidget(child);
QDrag *drag = new QDrag(this);
QPixmap image = child->grab(child->rect());
QMimeData *mimeData = new QMimeData;
mimeData->setData("data", "");
drag->setPixmap(image);
drag->setMimeData(mimeData);
drag->setHotSpot(event->pos() - child->pos());
drag->exec(Qt::MoveAction);
}
void dragdropwidget::mouseMoveEvent(QMouseEvent *event)
{
qDebug()<<"event->pos"<<event->pos();
}
四、结语
欢迎各方多多指教
QQ:519096571
e-mail:[email protected]