当前位置:网站首页>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(); }
边栏推荐
- 5分+的单基因泛癌纯生信思路!
- Clickhouse deployment and basic usage 1
- Why does the virtual machine Ping the host but not the virtual machine
- Adobe Photoshop using the box selection tool for selection tutorial
- Based on am335x development board arm cortex-a8 -- acontis EtherCAT master station development case
- 保险APP适老化服务评测分析2022第06期
- 计组-总复习
- ahk实现闹钟
- PHP短信通知+语音播报自动双呼
- New progress in the construction of meituan's Flink based real-time data warehouse platform
猜你喜欢

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

Beauty of script │ VBS introduction interactive practice
![[digital ic/fpga] booth multiplier](/img/42/3da3b1d3cc82cb9c0694241148011b.png)
[digital ic/fpga] booth multiplier

《opencv学习笔记》-- 感兴趣区域(ROI)、图像混合
[Architect (Part 41)] installation of server development and connection to redis database

u盘安装kali并且持久化

工具及方法 - 在Source Insight中使用代码格式化工具

math_ Summation and derivation of proportional series & derivation of sum and difference of equal powers / difference between two nth power numbers/

FreeRTOS overview and experience

Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!
随机推荐
我在深圳,到哪里开户比较好?现在网上开户安全么?
Analysis and understanding of Jieba stutter word segmentation principle HMM application in Chinese word segmentation and partial code reading
Understanding of homogeneous coordinates
Code is really - omnipotent! Refuse to fight
GTest从入门到入门
如何开发短信通知和语音功能医院信息系统(HIS系统)
C语言循环语句介绍(foe、while、do...while)
Google Earth engine (GEE) - how to add a legend in the map panel
[206] use PHP language to generate the code of go language
11+! Methylation modification patterns based on m6A regulatory factors in colon cancer are characterized by different tumor microenvironment immune spectra
广发证券靠谱吗?开证券账户安全吗?
ArrayList#subList这四个坑,一不小心就中招
Google ranging for PHP wechat development
《opencv学习笔记》-- 感兴趣区域(ROI)、图像混合
How to develop hospital information system (his) with SMS notification and voice function
【数字IC/FPGA】Booth乘法器
工具及方法 - 在Source Insight中使用代码格式化工具
Libuv的安装及运行使用
Tools and methods - use code formatting tools in source insight
Reading at night -- about microservices and containers