当前位置:网站首页>QT -- the qtabwidget supports dragging tabbar items
QT -- the qtabwidget supports dragging tabbar items
2022-06-24 12:05:00 【cc_ rong】
Catalog
demand
1、 Support drag exchange TabBar Label location
ui->tabWidget->setMovable(true);2、 Support will TabBar Item pull out QTabWidget Top display
3、 Support the... That will be pulled out TabBar Items are dragged back QTabWidget in
effect
Code

MainWindow
#include "mainwindow.h" #include "ui_mainwindow.h" #include "custom_tabWidget/custom_tabwidget.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); m_pTabWidget = new CustomTabWidget(this); ui->verticalLayout->addWidget(m_pTabWidget); 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"); } MainWindow::~MainWindow() { delete ui; } /*************************************/ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTextEdit> namespace Ui { class MainWindow; } class CustomTabWidget; class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; CustomTabWidget* m_pTabWidget; }; #endif // MAINWINDOW_HCustomTabBar#ifndef CUSTOM_TABBAR_H #define CUSTOM_TABBAR_H #include <QTabBar> #include <QObject> #include <QPoint> #include <QMouseEvent> class CustomTabBar : public QTabBar { Q_OBJECT public: CustomTabBar(QWidget *parent = nullptr); ~CustomTabBar(); protected: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); signals: void signalStartDragTab(int); void signalStopDrapTab(); private: bool m_bPressFlag; QPoint m_pointPress; QPoint m_pointRelease; }; #endif // CUSTOM_TABBAR_H /******************************/ #include "custom_tabbar.h" CustomTabBar::CustomTabBar(QWidget *parent):QTabBar(parent) { m_bPressFlag = false; setMovable(true); //this->setAcceptDrops(true); } CustomTabBar::~CustomTabBar() { } void CustomTabBar::mousePressEvent(QMouseEvent *event) { if(event->button() == Qt::LeftButton && currentIndex() >= 0) { m_bPressFlag = true; m_pointPress = event->pos(); } QTabBar::mousePressEvent(event); } void CustomTabBar::mouseMoveEvent(QMouseEvent *event) { if(m_bPressFlag && event->buttons()) { if(qAbs(m_pointPress.y() - event->pos().y()) > this->height() && !tabRect(this->currentIndex()).contains(event->pos())) { m_bPressFlag = false; if(this->count() != 1) { emit signalStartDragTab(this->currentIndex()); emit signalStopDrapTab(); } } } QTabBar::mouseMoveEvent(event); } void CustomTabBar::mouseReleaseEvent(QMouseEvent *event) { m_bPressFlag = false; m_pointRelease = event->pos(); if (qAbs(m_pointPress.y() - m_pointRelease.y()) > this->height()) { // if(this->count() != 1) { // emit signalStopDrapTab(); // } } QTabBar::mouseMoveEvent(event); }CustomTabWidget#ifndef CUSTOM_TABWIDGET_H #define CUSTOM_TABWIDGET_H #include <QDrag> #include <QTabWidget> #include <QMimeData> #include <QDragEnterEvent> #include <QMouseEvent> class CustomTabBar; class CustomPopupPage; class CustomTabWidget : public QTabWidget { public: CustomTabWidget(QWidget *parent = nullptr); ~CustomTabWidget(); private: void init(); protected: // void dragEnterEvent(QDragEnterEvent *event); // void dragMoveEvent(QDragMoveEvent *event); // void dropEvent(QDropEvent *event); // void mousePressEvent(QMouseEvent *event); // void mouseMoveEvent(QMouseEvent *event); // void mouseReleaseEvent(QMouseEvent *event); private slots: void addTabPage(const QPoint &pos); private: CustomTabBar* m_pTabBar; QWidget* m_pDragTab; // Drag and drop the displayed page int m_nTabIndex; // Tag Index QString m_strTitle; // Tag title CustomPopupPage* m_pPopupPage; }; #endif // CUSTOM_TABWIDGET_H /*******************************/ #include "custom_tabwidget.h" #include "custom_tabbar.h" #include "custom_popup_page.h" CustomTabWidget::CustomTabWidget(QWidget *parent): QTabWidget(parent), m_pTabBar(nullptr) { init(); } CustomTabWidget::~CustomTabWidget() { } void CustomTabWidget::init() { this->setAcceptDrops(true); m_pTabBar = new CustomTabBar(this); this->setTabBar(m_pTabBar); // Drag window connect(m_pTabBar, &CustomTabBar::signalStartDragTab, this, [&](int index) { m_nTabIndex = index; m_strTitle = this->tabText(index); m_pDragTab = this->widget(index); }); // Drag and drop to release the window connect(m_pTabBar, &CustomTabBar::signalStopDrapTab, this, [&]() { // this->removeTab(m_nTabIndex); m_pPopupPage = new CustomPopupPage(this); connect(m_pPopupPage, &CustomPopupPage::signalDragRelease, this, &CustomTabWidget::addTabPage); connect(m_pPopupPage, &CustomPopupPage::signalAddTab, this, [&](){ const int index = this->insertTab(m_nTabIndex, m_pDragTab, m_strTitle); // Switch to the current new page this->setCurrentIndex(index); }); m_pPopupPage->setContentWidget(m_pDragTab); m_pPopupPage->setWindowTitle(m_strTitle); m_pPopupPage->resize(m_pDragTab->size()); m_pDragTab->show(); m_pPopupPage->exec(); }); } void CustomTabWidget::addTabPage(const QPoint &pos) { const QPoint bar_pos = this->tabBar()->mapFromGlobal(pos); // If you drag it back tabbar Within the scope of , hold widget Take it out and put it back tab if(this->tabBar()->contentsRect().contains(bar_pos)) { const int index = this->insertTab(m_nTabIndex, m_pPopupPage->getContentWidget(), m_pPopupPage->windowTitle()); // Switch to the current new page this->setCurrentIndex(index); m_pPopupPage->disconnect(); m_pPopupPage->close(); } }CustomPopupPage#ifndef CUSTOM_POPUP_PAGE_H #define CUSTOM_POPUP_PAGE_H #include <QObject> #include <QWidget> #include <QDialog> #include <QEvent> #include <QMouseEvent> #include <QVBoxLayout> #include <QCloseEvent> class CustomPopupPage : public QDialog { Q_OBJECT public: CustomPopupPage(QWidget *parent = nullptr); ~CustomPopupPage(); void setContentWidget(QWidget *page); QWidget* getContentWidget(); protected: bool event(QEvent *event); void closeEvent(QCloseEvent *event); signals: void signalDragRelease(const QPoint &globalPos); void signalAddTab(); private: QWidget * m_page; }; #endif // CUSTOM_POPUP_PAGE_H /******************************************************/ #include "custom_popup_page.h" CustomPopupPage::CustomPopupPage(QWidget *parent): QDialog(parent) { m_page = nullptr; } CustomPopupPage::~CustomPopupPage() { } void CustomPopupPage::setContentWidget(QWidget *page) { if(!page) return; m_page = page; QVBoxLayout *layout = new QVBoxLayout(this); layout->setMargin(0); layout->addWidget(page); } QWidget *CustomPopupPage::getContentWidget() { return m_page; } bool CustomPopupPage::event(QEvent *event) { switch(event->type()) { case QEvent::MouseButtonRelease: case QEvent::NonClientAreaMouseButtonRelease: { QMouseEvent *e=static_cast<QMouseEvent*>(event); if(e && e->button()==Qt::LeftButton) { emit signalDragRelease(e->globalPos()); } } break; } return QDialog::event(event); } void CustomPopupPage::closeEvent(QCloseEvent *event) { emit signalAddTab(); }
边栏推荐
- [Architect (Part 41)] installation of server development and connection to redis database
- Qt: 判断字符串是否为数字格式
- Adobe Photoshop using the box selection tool for selection tutorial
- Google ranging for PHP wechat development
- 链接器 --- Linker
- What are the low threshold financial products in 2022? Not much money
- How to write controller layer code gracefully?
- 【老卫搞机】090期:键盘?主机?全功能键盘主机!
- 11+文章-机器学习打造ProTICS框架-深度揭示了不同分子亚型中肿瘤浸润免疫细胞对预后的影响
- 11+! 结肠癌中基于 m6A 调节因子的甲基化修饰模式以不同的肿瘤微环境免疫谱为特征
猜你喜欢

计组-总复习

万名校园开发者花式玩AI,亮点看这张图就够啦!

Axi low power interface

如何开发短信通知和语音功能医院信息系统(HIS系统)

Install Kali on the U disk and persist it
![[digital ic/fpga] booth multiplier](/img/42/3da3b1d3cc82cb9c0694241148011b.png)
[digital ic/fpga] booth multiplier

Qt: 判断字符串是否为数字格式

New progress in the construction of meituan's Flink based real-time data warehouse platform

@Requestbody annotation

我真傻,招了一堆只会“谷歌”的程序员!
随机推荐
9+!通过深度学习从结直肠癌的组织学中预测淋巴结状态
"Meng Hua Lu" is about to have a grand finale. It's better to learn it first than to look ahead!
Embedded must learn! Detailed explanation of hardware resource interface - based on arm am335x development board (Part 1)
不用做实验的6分+基因家族纯生信思路~
【206】使用php语言去生成go语言的代码
Install Kali on the U disk and persist it
math_ Summation and derivation of proportional series & derivation of sum and difference of equal powers / difference between two nth power numbers/
TP-LINK 1208 router tutorial (2)
C语言循环语句介绍(foe、while、do...while)
怎样申请打新债 开户是安全的吗
Nacos source code - configure automatic update
Google ranging for PHP wechat development
Multi gate mixture of experts and code implementation
Is it safe to apply for new bonds to open an account
Reading at night -- about microservices and containers
Qt: 判断字符串是否为数字格式
嵌入式必学!硬件资源接口详解——基于ARM AM335X开发板 (下)
单基因泛癌+简单实验就能发表7分+
GLOG从入门到入门
Analysis and understanding of Jieba stutter word segmentation principle HMM application in Chinese word segmentation and partial code reading