我是按照有个例子做的,但是他做的不太好,拖拽之后只是单独的窗口,直接上代码
.h
#ifndef CTABWIDGET_H
#define CTABWIDGET_H
#include
#include
class myTabBar:public QTabBar
{
Q_OBJECT
public:
myTabBar(QWidget *parent = NULL);
~myTabBar();
protected:
void mousePressEvent (QMouseEvent *e);
void mouseMoveEvent (QMouseEvent *e);
//void mouseReleaseEvent (QMouseEvent *e);
signals:
void starDragTab(int index);
void endDragTab();
private:
void drag();
bool m_isDrag;
QPoint m_mousePressPoint;
QPoint m_mouseReleasePoint;
};
class myTabWidget:public QTabWidget
{
Q_OBJECT
public:
myTabWidget(QWidget *parent = NULL);
~myTabWidget()
{
}
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
private slots:
void starDrag(int index);
void endDrag();
private:
myTabBar *m_pTabBar;
QString m_dragTabLabel;
QWidget *m_pDragWidget;
int m_dragIndex;
};
#endif // CTABWIDGET_H
.cpp
#include "tabwidget.h"
#include
#include
#include
#include
#include
#include
#include
myTabBar::myTabBar(QWidget *parent):QTabBar(parent), m_isDrag(false)
{
}
myTabBar::~myTabBar()
{
}
void myTabBar::mouseMoveEvent(QMouseEvent *e)
{
QTabBar::mouseMoveEvent(e);
int index = this->currentIndex();
//if (m_isDrag)
{
QPoint Point = e->pos();
//拖动的垂直距离大于Bar的高度的话就新建一个窗口,这个应该比较好理解啊
if(!tabRect(index).contains(Point))
//if (qAbs(m_mousePressPoint.y() - Point.y()) > this->height())
{
m_isDrag = false;
qDebug() << "mouse drag";
emit starDragTab(index);
}
else
{
qDebug() << "mouse not drag";
}
}
//else
{
qDebug() << "11111111111";
}
//QTabBar::mouseMoveEvent(e);
this->repaint();
}
void myTabBar::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
{
qDebug() << "mouse press";
m_isDrag = true;
m_mousePressPoint = e->pos();
this->setFocus();
}
QTabBar::mousePressEvent(e);
}
/*void myTabBar::mouseReleaseEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
{
m_mouseReleasePoint = e->pos();
if (qAbs(m_mousePressPoint.y() - m_mouseReleasePoint.y()) > this->height())
{
QWidget *p = qApp->widgetAt(e->pos());
emit endDragTab();
}
}
QTabBar::mouseReleaseEvent(e);
}*/
myTabWidget::myTabWidget(QWidget *parent/* = NULL*/):QTabWidget(parent)
{
m_pTabBar = new myTabBar(this);
//m_pTabBar->setMovable(true);
setTabBar(m_pTabBar); //设置自定义的Bar给TabWidget
m_pDragWidget = NULL;
connect(m_pTabBar,SIGNAL(starDragTab(int)),this,SLOT(starDrag(int)));
connect(m_pTabBar,SIGNAL(endDragTab()),this,SLOT(endDrag()));
this->setAcceptDrops(true);
//setMouseTracking(true);
}
void myTabWidget::starDrag(int index)
{
m_dragTabLabel = this->tabText(index);
m_pDragWidget = this->widget(index);
m_dragIndex = index;
QPixmap pix;
//pix = QPixmap::grabWidget(m_pDragWidget);
pix = QPixmap::grabWindow(this->winId());
if (pix.isNull())
{
int i;
}
QMimeData *mimeData = new QMimeData;
//mimeData->setText("drag tab");
mimeData->setUserData(0, (QObjectUserData *)m_pDragWidget);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pix.scaled(QSize(200,200))); //这里主要是想想QQ一样,可以拖动的过程显示被拖动窗口的图片。但是效果还不是很好
drag->exec();
}
void myTabWidget::endDrag()
{
myTabWidget *pWidget = new myTabWidget(NULL);
if (m_pDragWidget)
{
//新建一个独立的窗口
removeTab(m_dragIndex);
pWidget->addTab(m_pDragWidget,m_dragTabLabel);
pWidget->show();
}
}
void myTabWidget::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
QTabWidget::dragEnterEvent(event);
}
void myTabWidget::dragMoveEvent(QDragMoveEvent *event)
{
QTabWidget::dragMoveEvent(event);
}
void myTabWidget::dropEvent(QDropEvent *event)
{
QWidget *p = (QWidget *)event->mimeData()->userData(0);
QObject *pp = event->source();
//((myTabWidget *)pp)->tabBar()->clearFocus();
if(pp == (QObject *)this)
{
QTabWidget::dropEvent(event);
return QTabWidget::dropEvent(event);
}
this->addTab(p, "nima");
// QTabWidget::dropEvent(event);
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
namespace Ui {
class MainWindow;
}
class myTabWidget;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void dropEvent(QDropEvent *e) override;
private:
Ui::MainWindow *ui;
myTabWidget *m_pTabWidget;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tabwidget.h"
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setAcceptDrops(true);
QSplitter *p = new QSplitter(this);
m_pTabWidget = new myTabWidget(this);
//m_pTabWidget->setMovable(true);
m_pTabWidget->setTabsClosable(true);
m_pTabWidget->setTabShape(QTabWidget::Triangular);
//添加4个tab页
m_pTabWidget->addTab(new QTextEdit,"eidt 1");
m_pTabWidget->addTab(new QTextEdit,"eidt 2");
m_pTabWidget->addTab(new QTextEdit,"eidt 3");
m_pTabWidget->addTab(new QTextEdit,"eidt 4");
myTabWidget *m_pTabWidget1 = new myTabWidget(this);
//m_pTabWidget1->setMovable(true);
m_pTabWidget1->setTabsClosable(true);
m_pTabWidget1->setTabShape(QTabWidget::Triangular);
//添加4个tab页
m_pTabWidget1->addTab(new QTextEdit,"eidt 1");
m_pTabWidget1->addTab(new QTextEdit,"eidt 2");
m_pTabWidget1->addTab(new QTextEdit,"eidt 3");
m_pTabWidget1->addTab(new QTextEdit,"eidt 4");
p->addWidget(m_pTabWidget);
p->addWidget(m_pTabWidget1);
setCentralWidget(p);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::dropEvent(QDropEvent *e)
{
}
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
源码下载地址:https://download.csdn.net/download/wojiuguowei/10440774