#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
for(int i=0;i<8;i++){
cd[i] = new Card(this);
cd[i]->txt=QString::number(i+1);
connect(cd[i],&Card::sendSelf,this,&Widget::getObject);
connect(cd[i],&Card::sendNeedChange,this,&Widget::needChange);
connect(this,&Widget::sendPos,cd[i],&Card::getPos);
cd[i]->move(i%4*200,i/4*400);
}
}
Widget::~Widget()
{
delete ui;
}
void Widget::getObject(Card *w)
{
temp = w;
disconnect(this,&Widget::sendPos,w,&Card::getPos);
startP = cursor().pos()-this->pos();
yuanP = temp->pos();
yuanR = temp->geometry();
isMoving=true;
targetR = w->geometry();
}
void Widget::needChange(Card *w)
{
targetR = w->geometry();
// QPropertyAnimation *animation = new QPropertyAnimation(w,"geometry");
// animation->setDuration(300);
// animation->setStartValue(w->geometry());
// animation->setEndValue(yuanR);
// animation->start();
w->setGeometry(yuanR);
yuanR = targetR;
//isMoving=false;
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
temp->move(yuanP.x()+event->x()-startP.x(),yuanP.y()+event->y()-startP.y());
if(isMoving){
emit sendPos(event->pos());
}
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
connect(this,&Widget::sendPos,temp,&Card::getPos);
temp->raise();
qDebug()<<targetR;
temp->setGeometry(targetR);
isMoving=false;
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt自定义可拖拽交换位置的控件
共9个文件
cpp:3个
h:2个
user:1个
需积分: 18 22 下载量 114 浏览量
2023-02-21
13:35:52
上传
评论 1
收藏 8KB RAR 举报
温馨提示
qt实现的可拖拽、相互交换位置的控件,类似于三国杀中的拖动手牌可交换位置
资源推荐
资源详情
资源评论
收起资源包目录
DragTest.rar (9个子文件)
DragTest
widget.ui 667B
DragTest.pro 640B
widget.cpp 2KB
card.cpp 661B
main.cpp 230B
DragTest.pro.user 24KB
DragTest.pro.user.a523888 25KB
card.h 543B
widget.h 757B
共 9 条
- 1
资源评论
戏言zare
- 粉丝: 1121
- 资源: 30
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功