Tab角落控件位置占满
This commit is contained in:
parent
817db1bd53
commit
0f2030684e
5 changed files with 109 additions and 1 deletions
35
QProxyStyle/Lib/TabCornerStyle.py
Normal file
35
QProxyStyle/Lib/TabCornerStyle.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
Created on 2021年06月23日
|
||||||
|
@author: Irony
|
||||||
|
@site: https://pyqt.site , https://github.com/PyQt5
|
||||||
|
@email: 892768447@qq.com
|
||||||
|
@file: TabCornerStyle
|
||||||
|
@description:
|
||||||
|
"""
|
||||||
|
from PyQt5.QtCore import QRect
|
||||||
|
from PyQt5.QtWidgets import QProxyStyle, QStyle
|
||||||
|
|
||||||
|
|
||||||
|
class TabCornerStyle(QProxyStyle):
|
||||||
|
|
||||||
|
def subElementRect(self, element, option, widget):
|
||||||
|
try:
|
||||||
|
rect = super(TabCornerStyle, self).subElementRect(element, option, widget)
|
||||||
|
if element == QStyle.SE_TabWidgetRightCorner and rect.isValid():
|
||||||
|
# 标签tab的矩形范围
|
||||||
|
tab_rect = self.subElementRect(QStyle.SE_TabWidgetTabBar, option, widget)
|
||||||
|
# 内容面板的矩形范围
|
||||||
|
panel_rect = self.subElementRect(QStyle.SE_TabWidgetTabPane, option, widget)
|
||||||
|
ext_height = 2 * self.pixelMetric(QStyle.PM_TabBarBaseHeight, option, widget)
|
||||||
|
# 修正过后填充的矩形范围
|
||||||
|
cor_rect = QRect(tab_rect.x() + tab_rect.width() + ext_height, tab_rect.y() + ext_height,
|
||||||
|
panel_rect.width() - tab_rect.width() - 2 * ext_height,
|
||||||
|
tab_rect.height() - 2 * ext_height)
|
||||||
|
return cor_rect
|
||||||
|
return rect
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return QRect()
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
- 目录
|
- 目录
|
||||||
- [QTabWidget Tab文字方向](#1qtabwidget-tab文字方向)
|
- [QTabWidget Tab文字方向](#1qtabwidget-tab文字方向)
|
||||||
|
- [QTabWidget 角落控件位置](#2qtabwidget-角落控件位置)
|
||||||
|
|
||||||
## 1、QTabWidget Tab文字方向
|
## 1、QTabWidget Tab文字方向
|
||||||
[运行 TabTextDirection.py](TabTextDirection.py)
|
[运行 TabTextDirection.py](TabTextDirection.py)
|
||||||
|
@ -11,3 +12,14 @@
|
||||||
3. `drawControl` 绘制文字
|
3. `drawControl` 绘制文字
|
||||||
|
|
||||||
![TabTextDirection](ScreenShot/TabTextDirection.png)
|
![TabTextDirection](ScreenShot/TabTextDirection.png)
|
||||||
|
|
||||||
|
## 2、QTabWidget 角落控件位置
|
||||||
|
[运行 TabCornerWidget.py](TabCornerWidget.py)
|
||||||
|
|
||||||
|
1. 通过 `app.setStyle(TabCornerStyle())` 设置代理样式
|
||||||
|
2. `setCornerWidget` 设置自定义角落控件
|
||||||
|
|
||||||
|
原理是通过代理样式中对 `SE_TabWidgetRightCorner` 计算的结果进行校正,使得角落控件占满右边空白位置,
|
||||||
|
然后再配合自定义控件中使用 `QSpacerItem` 占据右边位置使得 + 号按钮居左,表现效果为 + 号按钮跟随标签的增加和减少
|
||||||
|
|
||||||
|
![TabCornerStyle](ScreenShot/TabCornerStyle.png)
|
BIN
QProxyStyle/ScreenShot/TabCornerStyle.png
Normal file
BIN
QProxyStyle/ScreenShot/TabCornerStyle.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 21 KiB |
60
QProxyStyle/TabCornerWidget.py
Normal file
60
QProxyStyle/TabCornerWidget.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
Created on 2021年06月23日
|
||||||
|
@author: Irony
|
||||||
|
@site: https://pyqt.site , https://github.com/PyQt5
|
||||||
|
@email: 892768447@qq.com
|
||||||
|
@file: TabCornerWidget
|
||||||
|
@description:
|
||||||
|
"""
|
||||||
|
from PyQt5.QtCore import pyqtSignal, Qt
|
||||||
|
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QSpacerItem, QSizePolicy, \
|
||||||
|
QTabWidget
|
||||||
|
|
||||||
|
from QProxyStyle.Lib.TabCornerStyle import TabCornerStyle
|
||||||
|
|
||||||
|
|
||||||
|
class TabCornerWidget(QWidget):
|
||||||
|
signalTabAdd = pyqtSignal()
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(TabCornerWidget, self).__init__(*args, **kwargs)
|
||||||
|
layout = QHBoxLayout(self)
|
||||||
|
layout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
self.buttonAdd = QPushButton('+', self, toolTip='添加新标签页', clicked=self.signalTabAdd.emit)
|
||||||
|
layout.addWidget(self.buttonAdd)
|
||||||
|
layout.addItem(QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
|
||||||
|
|
||||||
|
def resizeEvent(self, event):
|
||||||
|
super(TabCornerWidget, self).resizeEvent(event)
|
||||||
|
# 更新按钮高度
|
||||||
|
if hasattr(self, 'buttonAdd'):
|
||||||
|
self.buttonAdd.setFixedSize(self.height(), self.height())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import sys
|
||||||
|
|
||||||
|
app = QApplication(sys.argv)
|
||||||
|
app.setStyle(TabCornerStyle())
|
||||||
|
|
||||||
|
tab1 = QTabWidget()
|
||||||
|
cor1 = TabCornerWidget(tab1)
|
||||||
|
cor1.signalTabAdd.connect(lambda: tab1.addTab(QWidget(tab1), 'tab' + str(tab1.count() + 1)))
|
||||||
|
tab1.setCornerWidget(cor1, Qt.TopRightCorner)
|
||||||
|
tab1.show()
|
||||||
|
|
||||||
|
tab2 = QTabWidget()
|
||||||
|
tab2.setTabPosition(QTabWidget.South) # tab 标签方向
|
||||||
|
cor2 = TabCornerWidget(tab2)
|
||||||
|
cor2.signalTabAdd.connect(lambda: tab2.addTab(QWidget(tab2), 'tab' + str(tab2.count() + 1)))
|
||||||
|
tab2.setCornerWidget(cor2, Qt.BottomRightCorner)
|
||||||
|
tab2.show()
|
||||||
|
|
||||||
|
for i in range(10):
|
||||||
|
tab1.addTab(QWidget(tab1), 'tab' + str(i + 1))
|
||||||
|
tab2.addTab(QWidget(tab1), 'tab' + str(i + 1))
|
||||||
|
|
||||||
|
sys.exit(app.exec_())
|
|
@ -222,6 +222,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分
|
||||||
- [串口调试小助手](QSerialPort/SerialDebugAssistant.py)
|
- [串口调试小助手](QSerialPort/SerialDebugAssistant.py)
|
||||||
- [QProxyStyle](QProxyStyle)
|
- [QProxyStyle](QProxyStyle)
|
||||||
- [Tab文字方向](QProxyStyle/TabTextDirection.py)
|
- [Tab文字方向](QProxyStyle/TabTextDirection.py)
|
||||||
|
- [Tab角落控件位置占满](QProxyStyle/TabCornerWidget.py)
|
||||||
- [QMessageBox](QMessageBox)
|
- [QMessageBox](QMessageBox)
|
||||||
- [消息对话框倒计时关闭](QMessageBox/CountDownClose.py)
|
- [消息对话框倒计时关闭](QMessageBox/CountDownClose.py)
|
||||||
- [自定义图标等](QMessageBox/CustomColorIcon.py)
|
- [自定义图标等](QMessageBox/CustomColorIcon.py)
|
||||||
|
|
Loading…
Reference in a new issue