#include "CustomTabWidget.h"
#include <QApplication>
#include <QMouseEvent>
#include <QPixmap>
#include <QMimeData>
#include <QPainter>
#include <QCursor>
#include <QDrag>
#include <QDebug>
#include "CustomPopDialog.h"
CustomTabWidget::CustomTabWidget(QWidget *parent)
: QTabWidget(parent)
{
//ui.setupUi(this);
//Stylesheet does not work Solution
setAttribute(Qt::WA_StyledBackground);
initTabBar();
setMouseTracking(true);
connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(getCloseRequested(int)));
}
CustomTabWidget::~CustomTabWidget()
{
}
void CustomTabWidget::getCloseRequested(int value)
{
removeTab(value);
}
// add a page
int CustomTabWidget::addTab(QWidget* page)
{
return addTab(page, page->windowTitle());
}
int CustomTabWidget::addTab(QWidget* page, const QString& windowTitle)
{
if (!page)
return -1;
// This window is in pop-up status, and cannot be added repeatedly
if (m_QLPopedPage.contains(page))
{
return -2;
}
// !!! Set to release when calling close, and sometimes release when it cannot be closed
//page->setAttribute(Qt::WA_DeleteOnClose);
page->setWindowTitle(windowTitle); // widget title 和 tab title一致
const int index = QTabWidget::addTab(page, windowTitle);
setCurrentIndex(index);
emit updateTabWidgetPage(this);
return index;
}
int CustomTabWidget::addTab(QWidget* page, const QIcon& icon, const QString& windowTitle)
{
if (!page)
return -1;
// This window is in pop-up status, and cannot be added repeatedly
if (m_QLPopedPage.contains(page))
{
return -2;
}
// !!! Set to release when calling close, and sometimes release when it cannot be closed
//page->setAttribute(Qt::WA_DeleteOnClose);
page->setWindowTitle(windowTitle); // widget title 和 tab title一致
const int index = QTabWidget::addTab(page, icon, windowTitle);
setCurrentIndex(index);
emit updateTabWidgetPage(this);
return index;
}
// Add a fixed page, which cannot be dragged out
void CustomTabWidget::addFixedTab(QWidget* page)
{
if (!page)
return;
addTab(page);
setFixedTabPage(page);
}
//Set that a page cannot be closed and dragged out
void CustomTabWidget::setFixedTabPage(QWidget* page)
{
if (!page)
return;
setFixedTabPage(indexOf(page));
}
void CustomTabWidget::setFixedTabPage(int index)
{
if (isTabPageValid(index))
{
QWidget* page = this->widget(index);
if (page && !m_QLfixedPage.contains(page))
{
m_QLfixedPage.push_back(page);
//Do not display the close button, replace with nullptr
tabBar()->setTabButton(index, QTabBar::RightSide, nullptr);
}
}
}
// Remove tab
void CustomTabWidget::removeTab(int index)
{
if (isTabPageValid(index))
{
QWidget* page = this->widget(index);
//Judge whether it is fixed and not deleted
if (!page || m_QLfixedPage.contains(page))
return;
//The removeTab is only removed from the tabbar and not released
QTabWidget::removeTab(index);
emit updateTabWidgetPage(this);
//You can call delete to release tab page or set the tab page to WA_ DeleteOnClose, release on close
//delete page;
//page->close();
}
}
// Remove tab
void CustomTabWidget::removeTab(QWidget* page)
{
if (!page)
return;
removeTab(indexOf(page));
}
// Remove current tab
void CustomTabWidget::removeCurrentTab()
{
removeTab(currentIndex());
}
// Clear all pages (no include fixed pages), only the tabs on the tabbar will be cleared, and the pop-up will not be cleared
void CustomTabWidget::clearTab()
{
const int tab_count = this->count();
//Delete from the back to the front, so that the index position is fixed
for (int index = tab_count - 1; index >= 0; index--)
{
removeTab(index);
}
}
// Clear all pages (include fixed pages), only the tabs on the tabbar will be cleared, and the pop-up will not be cleared
void CustomTabWidget::clearAllTab()
{
//全部移除
while (this->count() > 0)
{
//QWidget *page=this->widget(0);
QTabWidget::removeTab(0); // 直接调用QTabWidget函数,不判断是否为固定标签
// if(page)
// page->close();
}
m_QLfixedPage.clear();
}
// page is valid
bool CustomTabWidget::isTabPageValid(int index) const
{
if (index < 0 || index >= this->count())
return false;
return true;
}
// page is valid
bool CustomTabWidget::isTabPageValid(QWidget* page) const
{
if (!page)
return false;
return isTabPageValid(indexOf(page));
}
bool CustomTabWidget::isTabPagePoped(QWidget* page) const
{
if (!page)
return false;
// 是否包含
return m_QLPopedPage.contains(page);
}
void CustomTabWidget::updateTabWidgetShow(std::map<QWidget*, bool> tabWidgetState)
{
for (auto it = tabWidgetState.begin(); it != tabWidgetState.end(); it++)
{
if (it->second)
{
QWidget* widget = it->first;
if (isTabPagePoped(widget))
{
emit closeDialogWidget(widget);
}
else
{
addTab(widget);
}
}
else
{
QWidget* widget = it->first;
removeTab(widget);
}
}
}
void CustomTabWidget::showEvent(QShowEvent* event)
{
QTabWidget::showEvent(event);
//When initializing, set the existing settings to be released when closing
for (int index = 0; index < this->count(); index++)
{
QWidget* page = this->widget(index);
if (page)
page->setAttribute(Qt::WA_DeleteOnClose);
}
}
void CustomTabWidget::mousePressEvent(QMouseEvent* event)
{
int a = 0;
}
void CustomTabWidget::mouseMoveEvent(QMouseEvent* event)
{
int a = 0;
}
// Set whether the label can be closed, and connect the signal and slot when it can be closed
void CustomTabWidget::setTabsClosable(bool flag)
{
if (flag)
{
connect(m_pBar, SIGNAL(QTabBar::tabCloseRequested(int)), this, SLOT(MyTabWidget::removeTab(int)));
}
QTabWidget::setTabsClosable(flag);
}
/**************************************/
void CustomTabWidget::initTabBar()
{
CustomTabBar* m_pBar = new CustomTabBar(this);
this->m_pBar = m_pBar;
setTabBar(m_pBar);
//Drag outside - mouse not released yet
connect(m_pBar, &CustomTabBar::beginDragOut, this, [this, m_pBar](int index) {
if (!isTabPageValid(index))
{
qDebug() << "beginDragOut index error";
return;
}
QWidget* drag_tab = this->widget(index);
//Fixed tab will not be dragged out
if (!drag_tab || m_QLfixedPage.contains(drag_tab))
{
qDebug() << "beginDragOut fixed tab";
return;
}
//Drag the current page as a snapshot
//Size added with title block and border
QPixmap pixmap(drag_tab->size() + QSize(2, 31));
pixmap.fill(Qt::transparent); //Set image transparency
QPainter painter(&pixmap);
if (painter.isActive())
{
//I want to make a title bar above the content here
//But i can't get the image of the default title bar. Just draw a rectangular box
//If the external theme color is set, it needs to be changed
QRect title_rect{ 0,0,pixmap.width(),30 };
painter.fillRect(title_rect, Qt::white);
painter.drawText(title_rect, Qt::AlignLeft | Qt::AlignVCenter, " " + drag_tab->windowTitle());
/*Return an adjusted QRec
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该功能由QT插件实现的一个带界面的DLL,该dll实现了如下功能: 1、在主界面侧边添加工具栏框架 2、可以收缩和展开工具栏 3、可以在工具栏中插入各种小工具界面 4、可以在工具栏中删除界面 5、可以在工具栏中把子界面拖出来和放回去 用法链接:https://blog.csdn.net/cs1395293598/article/details/134511164?spm=1001.2014.3001.5501 微博主页:https://menghui666.blog.csdn.net/
资源推荐
资源详情
资源评论
收起资源包目录
TabBar.zip (18个子文件)
CustomTabWidget.cpp 13KB
TabBar.h 1KB
CustomTabBar.cpp 3KB
tabbar_global.h 229B
TabBarPlugin.h 1014B
TabBar.vcxproj.user 560B
CustomTabWidget.h 4KB
CustomPopDialog.cpp 3KB
TabBar.vcxproj 6KB
TabBarWidget.h 838B
TabBarWidget.cpp 4KB
CustomPopDialog.h 949B
TabBar.vcxproj.filters 2KB
TabBar.json 300B
TabBar.cpp 818B
.gitignore 28B
CustomTabBar.h 850B
TabBarWidget.ui 904B
共 18 条
- 1
资源评论
梦回阑珊
- 粉丝: 2453
- 资源: 619
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功