-
Notifications
You must be signed in to change notification settings - Fork 364
/
MyTabBar.cpp
59 lines (52 loc) · 1.68 KB
/
MyTabBar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "MyTabBar.h"
#include <QApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QDebug>
MyTabBar::MyTabBar(QWidget *parent)
: QTabBar(parent)
{
setAttribute(Qt::WA_StyledBackground);
//tab页签可以拖拽交换位置
setMovable(true);
//tab页签显示关闭按钮
setTabsClosable(true);
}
void MyTabBar::mousePressEvent(QMouseEvent *event)
{
QTabBar::mousePressEvent(event);
if(event->button()==Qt::LeftButton&¤tIndex()>=0)
{
//保存状态
//pressPos=event->pos();
theDragPress=true;
}
}
void MyTabBar::mouseMoveEvent(QMouseEvent *event)
{
QTabBar::mouseMoveEvent(event);
//move的时候button为NoButton,但是button's里有
if(theDragPress&&event->buttons())
{
//是否脱离了tabbar的范围
if(!theDragOut&&!contentsRect().contains(event->pos())){
theDragOut=true;
emit beginDragOut(this->currentIndex());
//QDrag.exec后就不会触发release了,自己手动触发
//不过他好像还是在鼠标弹起之后才会进行动画,待解决
QMouseEvent *e=new QMouseEvent(QEvent::MouseButtonRelease,
this->mapFromGlobal(QCursor::pos()),
Qt::LeftButton,
Qt::LeftButton,
Qt::NoModifier);
//mouseReleaseEvent(event);
QApplication::postEvent(this,e);
}
}
}
void MyTabBar::mouseReleaseEvent(QMouseEvent *event)
{
QTabBar::mouseReleaseEvent(event);
theDragPress=false;
theDragOut=false;
}