《QT实用小工具·六十七》QTabWidget实现的炫酷标签工具栏

1、概述
源码放在文章末尾

该项目基于QTabWidget和QTabBar实现了灵活的标签工具栏,主要包含如下功能:
1、标签栏可以收起,可以展开
2、可以在标签栏中添加新的标签界面
3、可以从标签工具栏中把界面拖出来,也可以拖回去
4、关闭拖出来的界面会自动回到标签工具栏
5、可以调换标签工具栏中界面的顺序

项目demo演示如下所示:
在这里插入图片描述
使用方式:
1、创建标签栏对象

TabBarPlugin* tabBarPlugin = new TabBar();

2、把标签栏放入布局

ui.centralWidget->layout()->addWidget(tabBarPlugin->getWidget());

3、在标签栏中添加多个界面

tabBarPlugin->addWidget(widget1, "testDemo1", QIcon());
tabBarPlugin->addWidget(widget2, "testDemo2", QIcon());
tabBarPlugin->addWidget(widget3, "testDemo3", QIcon());

项目部分代码如下所示:

#pragma once

#include <QTabWidget>
//#include "ui_CustomTabWidget.h"
#include "CustomTabBar.h"

/**
 * @brief 自定义TabWidget
 * @details setTabBar为protected接口,要修改tabbar就得继承下
 *  如果标签页关闭后会释放
 *  因此只需要删除tab不关闭窗口时需要调用 remove,clear
 *  因此需要删除tab同时关闭窗口时需要调用 close
 * 
 *  保存弹出的widget指针,因为弹出的窗口有时候不会关闭,可能会导致重复添加
 *  在窗口弹出的时候添加到m_QLPopedPage,在窗口恢复以及关闭的时候从m_QLPopedPage移除
 *  当删除标签页的时候,判断是否为弹出的,弹出的不删除
 *  当添加标签页的时候,判断是否为弹出的,弹出的不添加
 *
 *  If the tab is closed, it will be released
 *  You only need to delete the tab without closing the window. You need to call remove, clear
 *  When you need to delete the tab and close the window, you need to call close
 *
 *  Save the pop-up widget pointer, because the pop-up window will not be closed sometimes, which may lead to repeated addition
 *  Add to m_QLPopedPage when the window pops up, and remove from m_QLPopedPage when the window restores and closes
 *  When deleting a tab, judge whether it is pop-up, and the pop-up will not be deleted
 *  When adding a tab, judge whether it is pop-up, and do not add the pop-up
 * 
 */

class CustomTabWidget : public QTabWidget
{
	Q_OBJECT

public:
	CustomTabWidget(QWidget *parent = nullptr);
	~CustomTabWidget();

public:
    // add a page  
    int addTab(QWidget* page);
    int addTab(QWidget* page, const QString&);
    int addTab(QWidget* page, const QIcon& icon, const QString& windowTitle);

    //Add a page that cannot be closed and dragged out
    //appendNormalPage+setFixedPage
    void addFixedTab(QWidget* page);
    //void addFixedTab(QWidget* page, const QString&);

    //Set that a page cannot be closed and dragged out
    void setFixedTabPage(QWidget* page);
    void setFixedTabPage(int index);

    // Remove a page and do not close it
    void removeTab(QWidget* page);
    void removeTab(int index);

    // Remove current page
    void removeCurrentTab();

    //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 clearTab();

    //Clear all pages (include fixed pages), only the tabs on the tabbar will be cleared, and the pop-up will not be cleared
    void clearAllTab();

    // tab is valid
    bool isTabPageValid(int index) const;
    bool isTabPageValid(QWidget* page) const;

    // Check whether a page has popped up
    //bool isTabPagePoped(int index) const;
    bool isTabPagePoped(QWidget* page) const;

    // Move a page from the tab and record the widget pointer to the pop-up list
    void popTab(QWidget* page);
    // Pop up a page and create a new window to place the widget
    void popPage(QWidget* page);

    // Set whether the label can be closed, and connect the signal and slot when it can be closed
    void setTabsClosable(bool flag);

signals:
    void updateTabWidgetPage(QTabWidget*);

private slots:
    void isDialogIntoTabBar(QPoint point, QDialog* dialog);

protected:
    void showEvent(QShowEvent* event);
    void mousePressEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;
private:
    CustomTabBar* m_pBar = nullptr;
    void initTabBar();

private:
	//Ui::CustomTabWidgetClass ui;

     // Save tab pages that cannot be dragged out (such as home page)
    QList<QWidget*> m_QLfixedPage;
    // Save the dragged tab
    QList<QWidget*> m_QLPopedPage;
};

源码下载

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦回阑珊

一毛不嫌多,一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值