#include "customtabwidget.h"
#include <QStackedWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QApplication>
#include <QPainter>
#include <QPixmap>
#include <QMimeData>
#include <QDrag>
#include <QFrame>
#include <QDropEvent>
#include <QDebug>
#include "customtabbar.h"
CustomTabWidget::CustomTabWidget(QWidget *parent)
: QWidget(parent)
{
m_pTabBar = new CustomTabBar(this);
QHBoxLayout *pHLayout = new QHBoxLayout;
pHLayout->addWidget(m_pTabBar);
pHLayout->addStretch();
m_pStackedWidget = new QStackedWidget(this);
QVBoxLayout *pVLayout = new QVBoxLayout(this);
pVLayout->addLayout(pHLayout);
pVLayout->addWidget(m_pStackedWidget);
pVLayout->setStretch(1, 1);
pVLayout->setMargin(0);
pVLayout->setSpacing(0);
connect(m_pTabBar, SIGNAL(currentChanged(int)), this, SLOT(setCurrentIndex(int)));
setAcceptDrops(true);
m_pStackedWidget->setStyleSheet("QStackedWidget{border: 1px solid #000000;}");
}
CustomTabWidget::~CustomTabWidget()
{
}
void CustomTabWidget::addTab(QWidget *widget, const QString &strTab)
{
m_pTabBar->addTab(strTab);
m_pStackedWidget->addWidget(widget);
widget->setAcceptDrops(true);
}
void CustomTabWidget::addTab(QWidget *page, const QIcon &icon, const QString &label)
{
m_pTabBar->addTab(icon, label);
m_pStackedWidget->addWidget(page);
}
void CustomTabWidget::addTabs(CustomTabWidget *pOtherTab)
{
for (int i = 0; i < pOtherTab->count(); i++)
{
addTab(pOtherTab->tabWidget(0), pOtherTab->tabText(i));
}
}
int CustomTabWidget::count() const
{
return m_pTabBar->count();
}
int CustomTabWidget::currentIndex() const
{
return m_pTabBar->currentIndex();
}
QWidget * CustomTabWidget::currentWidget() const
{
return m_pStackedWidget->currentWidget();
}
int CustomTabWidget::insertTab(int index, QWidget *page, const QString &label)
{
m_pTabBar->insertTab(index, label);
return m_pStackedWidget->insertWidget(index, page);
}
int CustomTabWidget::insertTab(int index, QWidget *page, const QIcon &icon, const QString &label)
{
m_pTabBar->insertTab(index, icon, label);
return m_pStackedWidget->insertWidget(index, page);
}
void CustomTabWidget::removeTab(int index)
{
m_pTabBar->removeTab(index);
m_pStackedWidget->removeWidget(m_pStackedWidget->widget(index)); //widget is not deleted but simply removed from the stacked layout, causing it to be hidden.
if (m_pTabBar->count() == 0)
{
emit tabIsEmpty();
if (m_bAutoDelete)
{
hide();
deleteLater();
}
}
}
QString CustomTabWidget::tabText(int index) const
{
return m_pTabBar->tabText(index);
}
QWidget * CustomTabWidget::tabWidget(int index) const
{
return m_pStackedWidget->widget(index);
}
void CustomTabWidget::setAutoHideTabBar(bool bHide)
{
m_bAutoHideBar = bHide;
}
void CustomTabWidget::setCurrentIndex(int index)
{
m_pStackedWidget->setCurrentIndex(index);
emit currentTabChanged(m_pTabBar->tabText(index));
}
void CustomTabWidget::setAutoDelete(bool bAuto)
{
m_bAutoDelete = bAuto;
}
void CustomTabWidget::mousePressEvent(QMouseEvent *event)
{
QWidget::mousePressEvent(event);
if (event->button() == Qt::LeftButton)
m_dragStartPos = event->pos();
}
void CustomTabWidget::mouseMoveEvent(QMouseEvent *event)
{
QWidget::mouseMoveEvent(event);
}
void CustomTabWidget::paintEvent(QPaintEvent *event)
{
if (m_bAutoHideBar)
{
m_pTabBar->setVisible(m_pTabBar->count() > 1);
}
}
void CustomTabWidget::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
void CustomTabWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("application/itemdata"))
{
QByteArray itemData = event->mimeData()->data("application/itemdata");
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
long long llPtrParent;
QString strTab;
long long llPtrChild;
int nTabIndex;
dataStream >> llPtrParent >> strTab >> llPtrChild >> nTabIndex;
CustomTabWidget *tabWidgetFrom = (CustomTabWidget *)llPtrParent;
QWidget *child = (QWidget *)llPtrChild;
if (child)
{
addTab(child, strTab);
event->acceptProposedAction();
}
}
else
{
event->ignore();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
TabWidgetDemo.zip
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共8个文件
cpp:4个
h:3个
ui:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 0 58 下载量 77 浏览量
2021-01-30
17:18:44
上传
评论 1
收藏 5KB ZIP 举报
温馨提示
我自己实现了一个可以拖出,拖入的自定义选项卡窗口类——CustomTabWidget。此demo主要通过QTabBar和QStackedWidget来实现QTabWidget的基本功能。CustomTabBar继承QTabBar,重载了鼠标事件和拖拽实现,来实现标签页拖出的功能。
资源详情
资源评论
资源推荐
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 8 条
- 1
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
MrMa7ic
- 粉丝: 11
- 资源: 2
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 以下是一些适用于英语六级作文的万能句型模板,涵盖了引言、正文和结论部分的各类表达方式.docx
- MATLAB中的非线性规划
- 进行C语言面试资格确认是招聘过程中一个重要的步骤,目的是确保候选人具备足够的C语言编程能力和知识.docx
- Java 轻量级的集群负载均衡设计
- 纹身师个人网站模板.jpg
- 在C语言中,连接两个字符串(即将一个字符串附加到另一个字符串的末尾)通常可以使用标准库中的 `strcat` 函数.docx
- 数据库管理工具:dbeaver-ce-23.1.1-stable.x86-64.rpm
- 以下是几个具体竞赛题目的详细解答,包括建模思路、方法和步骤 .docx
- 一份关于全国大学生建模大赛的相关教程!!
- 以下是关于计算机网络和现代通信组网的详细教程、案例和相关项目的推荐.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论0