pyQt5中将QTabWidget的tab标签页设置为竖直,文字为横向,代码如何编写
时间: 2024-01-22 09:17:24 浏览: 29
您好!感谢您的提问。以下是将QTabWidget的tab标签页设置为竖直,文字为横向的代码:
```python
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QTabWidget, QTabBar
class CustomTabBar(QTabBar):
def paintEvent(self, event):
painter = self.parent().style().proxy()
option = painter.styleOptionTabBarBase()
painter.drawPrimitive(QStyle.PE_FrameTabBarBase, option)
for index in range(self.count()):
painter.save()
painter.translate(self.tabRect(index).topLeft())
painter.rotate(-90)
tab_option = self.tabText(index)
painter.drawText(0, 0, self.tabRect(index).width(), self.tabRect(index).height(),
Qt.AlignVCenter | Qt.TextWordWrap, tab_option)
painter.restore()
class CustomTabWidget(QTabWidget):
def __init__(self, parent=None):
super(CustomTabWidget, self).__init__(parent)
self.tabBar().setTabButton(0, QTabBar.RightSide, None)
self.setTabBar(CustomTabBar(self))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
app.setStyle("fusion")
w = CustomTabWidget(None)
w.addTab(QWidget(), "Tab 1")
w.addTab(QWidget(), "Tab 2")
w.addTab(QWidget(), "Tab 3")
w.show()
sys.exit(app.exec_())
```
代码中我们创建了一个继承自QTabBar的CustomTabBar类,并覆盖了其paintEvent方法,在方法中使用QPainter来对tab进行绘制。为了让tab变为竖直,我们首先旋转Painter对象的状态。同时为了让文字内容正确显示,我们需要在paintEvent方法中对tab的文字进行处理。
最后,我们在CustomTabWidget类中将所创建的CustomTabBar对象设置为tab bar,并将原有的标签页button隐藏即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)